Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #75251
| X-Received | by 10.182.213.105 with SMTP id nr9mr11917736obc.36.1406399372840; Sat, 26 Jul 2014 11:29:32 -0700 (PDT) |
|---|---|
| X-Received | by 10.50.66.179 with SMTP id g19mr300418igt.3.1406399372650; Sat, 26 Jul 2014 11:29:32 -0700 (PDT) |
| Path | csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.glorb.com!h18no8145946igc.0!news-out.google.com!eg1ni5igc.0!nntp.google.com!h18no4796125igc.0!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail |
| Newsgroups | comp.lang.python |
| Date | Sat, 26 Jul 2014 11:29:32 -0700 (PDT) |
| Complaints-To | groups-abuse@google.com |
| Injection-Info | glegroupsg2000goo.googlegroups.com; posting-host=101.63.66.74; posting-account=mz2-UgoAAABhHsO0mzb5w7wo3Dt0FOo5 |
| NNTP-Posting-Host | 101.63.66.74 |
| User-Agent | G2/1.0 |
| MIME-Version | 1.0 |
| Message-ID | <23517efd-6d71-4182-9f18-7aaae3b95afb@googlegroups.com> (permalink) |
| Subject | Return class. |
| From | Satish ML <satishmlwizpro@gmail.com> |
| Injection-Date | Sat, 26 Jul 2014 18:29:32 +0000 |
| Content-Type | text/plain; charset=ISO-8859-1 |
| Xref | csiph.com comp.lang.python:75251 |
Show key headers only | View raw
Hi,
What does "return Wrapper" do in the following piece of code? Which method does it invoke?
I mean "return Wrapper" invokes __init__ method?
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
Actual program:
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())
print(bob.name)
print(bob.pay())
print([bob.fetches, sue.fetches])
Back to comp.lang.python | Previous | Next — Next in thread | Find similar | Unroll thread
Return class. Satish ML <satishmlwizpro@gmail.com> - 2014-07-26 11:29 -0700
Re: Return class. Chris Angelico <rosuav@gmail.com> - 2014-07-27 04:40 +1000
Re: Return class. Satish ML <satishmlwizpro@gmail.com> - 2014-07-26 12:02 -0700
Re: Return class. Satish ML <satishmlwizpro@gmail.com> - 2014-07-26 12:03 -0700
Re: Return class. Satish ML <satishmlwizpro@gmail.com> - 2014-07-26 12:04 -0700
Re: Return class. Chris Angelico <rosuav@gmail.com> - 2014-07-27 05:16 +1000
Re: Return class. Satish ML <satishmlwizpro@gmail.com> - 2014-07-26 12:21 -0700
Re: Return class. Satish ML <satishmlwizpro@gmail.com> - 2014-07-26 12:15 -0700
csiph-web