-
Notifications
You must be signed in to change notification settings - Fork 635
Expand file tree
/
Copy pathP12_GetterSetterMethods.py
More file actions
37 lines (25 loc) · 961 Bytes
/
P12_GetterSetterMethods.py
File metadata and controls
37 lines (25 loc) · 961 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
'''
Author: AMIT PATHAK
In this example, we will the getter and setter methods in python class.
Private variables of a class cannot be accessed outside the class using the class object.
In order to access or manipulate these variables, we make use of getter and setter methods respectively.
'''
class CreditCard(object):
def __init__(self, number, new_pin):
self.card_number = number
self.__pin = new_pin # Private Variable
def get_pin(self):
return self.__pin
def set_pin(self, new_pin):
self.__pin = new_pin
if __name__ == '__main__':
cc = CreditCard(number=514235895214, new_pin=1234)
print(cc.card_number)
### Output: 514235895214
#print(cc.__pin)
### Output: AttributeError: 'CreditCard' object has no attribute '__pin'
print(cc.get_pin())
### Output: 1234
cc.set_pin(new_pin=8745) # Set a new pin to 8745
print(cc.get_pin())
### Output: 8745