-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetattr-delegate.py
45 lines (35 loc) · 1.16 KB
/
getattr-delegate.py
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
class Person:
def __init__(self, name, job=None, pay=0):
self.name = name
self.job = job
self.pay = pay
def lastName(self):
return self.name.split()[-1]
def giveRaise(self, percent):
self.pay = int(self.pay * (1 + percent))
def __repr__(self):
return '[Person: %s, %s]' % (self.name, self.pay)
class Manager:
def __init__(self, name, pay):
self.person = Person(name, 'mgr', pay)
def giveRaise(self, percent, bonus=.10):
self.person.giveRaise(percent + bonus)
def __getattribute__(self, item):
print('**', item)
person = object.__getattribute__(self, 'person')
if item == 'giveRaise':
return lambda percent: person.giveRaise(percent + .10)
else:
return getattr(person, item)
def __repr__(self):
person = object.__getattribute__(self, 'person')
return str(person)
if __name__ == '__main__':
sue = Person('Sue Jones', job='dev', pay=100000)
print(sue.lastName())
sue.giveRaise(.10)
print(sue)
tom = Manager('Tom Jones', 50000)
print(tom.lastName())
tom.giveRaise(.10)
print(tom)