forked from abhi1540/PythonConceptExamples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgetter_setter.py
More file actions
139 lines (101 loc) · 2.88 KB
/
getter_setter.py
File metadata and controls
139 lines (101 loc) · 2.88 KB
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
class Employee(object):
def __init__(self, emp_id, first_name, last_name, salary, age):
self._emp_id = emp_id
self._first_name = first_name
self._last_name = last_name
self._salary = salary
self._age = age
@property
def emp_id(self):
return self._emp_id
@emp_id.setter
def emp_id(self, emp_id):
self._emp_id = emp_id
@property
def first_name(self):
return self._first_name
@first_name.setter
def first_name(self, first_name):
self._first_name = first_name
@property
def last_name(self):
return self._last_name
@last_name.setter
def last_name(self, last_name):
self._last_name = last_name
@property
def salary(self):
return self._salary
@salary.setter
def salary(self, salary):
self._salary = salary
@property
def age(self):
return self._age
@age.setter
def age(self, age):
if age > 18:
self._age = age
else:
raise ValueError("Enter age greater than 18")
def __str__(self):
return '{}-{}-{}-{}-{}'.format(self._emp_id, self._first_name, self._last_name, self._salary, self._age)
obj = Employee(1, 'abhisek', 'gantait', 10000, 28)
obj.__age = 30
print(obj) ## it will print 28 because you can't set the value outside
# output: 1-abhisek-gantait-10000-28
# MyClass.__private just becomes MyClass._MyClass__private
class A(object):
def __method(self):
print("I'm a method in A")
def method(self):
self.__method()
class B(A):
def __method(self):
print("I'm a method in B")
a = A()
a.method() #I'm a method in A
b = B()
b.method() #I'm a method in A
# def our_decorator(func):
# def function_wrapper(x):
# print("Before calling " + func.__name__)
# func(x)
# print("After calling " + func.__name__)
#
# return function_wrapper
#
#
# def foo(x):
# print("Hi, foo has been called with " + str(x))
#
#
# print("We call foo before decoration:")
# foo("Hi")
#
# print("We now decorate foo with f:")
# foo = our_decorator(foo)
#
# print("We call foo after decoration:")
# foo(42)
def log_instance(my_log):
def outerwrapper(cls_):
def wrapper_method(self):
my_log.info(
'Instance of {name} @ {id:X}: {dict}'.format(name=cls_.__name__, id=id(self), dict=repr(self.__dict__)))
cls_.log_instance = wrapper_method
cls_.log_instance.__name__ = 'log_instance'
cls_.log_instance.__doc__ = 'Generate log message with the details of this instance'
return cls_
return outerwrapper
import logging
mylog = logging.getLogger('my_logger')
mylog.setLevel(logging.INFO)
@log_instance(mylog)
class Hello(object):
def __init__(self, str):
self.str = str
def speak(self):
print('Hello {}'.format(self.str))
inst = Hello('Tony')
inst.speak()