Return class.
Satish ML
satishmlwizpro at gmail.com
Sat Jul 26 15:21:02 EDT 2014
Hi,
Which lines of code prints [4] and [4, 5, 6, 7] in the output?
Output:
CARST
Trace: display
1
Spam!Spam!Spam!Spam!Spam!Spam!Spam!Spam!
[1]
Trace: name
1
Bob
Trace: pay
2
2000
Trace: name
1
Sue
Trace: pay
2
6000
Trace: name
3
Bob
Trace: pay
4
2000
[4, 2]
INSIDE MyList
[1, 2, 3]
Trace: append
1
[4]
Trace: append
1
[4, 5, 6, 7]
Actual Code:
def Tracer(aClass):
class Wrapper:
def __init__(self, *args, **kargs):
self.fetches = 0
self.wrapped = aClass(*args, **kargs)
def __getattr__(self, attrname):
print('Trace: ' + attrname)
self.fetches += 1
print(self.fetches)
return getattr(self.wrapped, attrname)
return Wrapper
@Tracer
class Spam:
def __init__(self, *args):
print(*args)
def display(self):
print('Spam!' * 8)
@Tracer
class Person:
def __init__(self, name, hours, rate):
self.name = name
self.hours = hours
self.rate = rate
def pay(self):
return self.hours * self.rate
food = Spam("CARST")
food.display()
print([food.fetches])
bob = Person('Bob', 40, 50)
print(bob.name)
print(bob.pay())
print('')
sue = Person('Sue', rate=100, hours=60)
print(sue.name)
print(sue.pay())
Another module that is producing output:
from tracer import Tracer
@Tracer
class MyList(list):
def __init__(self, *args):
print("INSIDE MyList")
print(*args)
x = MyList([1, 2, 3])
x.append(4)
print(x.wrapped)
WrapList = Tracer(list)
x = WrapList([4, 5, 6])
x.append(7)
print(x.wrapped)
More information about the Python-list
mailing list