X-Received: by 10.68.137.9 with SMTP id qe9mr18072385pbb.0.1447521814428; Sat, 14 Nov 2015 09:23:34 -0800 (PST) X-Received: by 10.50.72.72 with SMTP id b8mr180028igv.2.1447521814397; Sat, 14 Nov 2015 09:23:34 -0800 (PST) Path: csiph.com!au2pb.net!usenet.blueworldhosting.com!feeder01.blueworldhosting.com!peer03.iad.highwinds-media.com!news.highwinds-media.com!feed-me.highwinds-media.com!border1.nntp.dca1.giganews.com!nntp.giganews.com!i2no2355200igv.0!news-out.google.com!f6ni2461igq.0!nntp.google.com!i2no2355197igv.0!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail Newsgroups: comp.lang.python Date: Sat, 14 Nov 2015 09:23:33 -0800 (PST) Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=50.100.117.144; posting-account=SZ_svQkAAACWRFG2bDA-zgq8ILyl4-vo NNTP-Posting-Host: 50.100.117.144 User-Agent: G2/1.0 MIME-Version: 1.0 Message-ID: Subject: What is wrong this wrapper (decorator)? From: fl Injection-Date: Sat, 14 Nov 2015 17:23:34 +0000 Content-Type: text/plain; charset=ISO-8859-1 Lines: 61 X-Received-Bytes: 2675 X-Received-Body-CRC: 2265311661 Xref: csiph.com comp.lang.python:98821 Hi, I follow a tutorial to learn decorator: http://simeonfranklin.com/blog/2012/jul/1/python-decorators-in-12-steps/ I use Enthought Canopy to run the following code. It is really strange that the wrapper does not take effect. In fact, I go back to the basic way (not with @): wrapper(sub(two, one)) Out[38]: When I use the non-wrapper mode, it has Coord print out. If I use wrapper, it has nothing to print out. Due to no debug mode help, I don't see anything wrong yet. (add(two, one)) # nothing print out (sub(two, three)) Out[43]: Coord:-- {'y': 300, 'x': 400} # correct is here Anyone can help? Thanks, ............ class Coordinate(object): def __init__(self, y, x): self.y = y self.x = x def __repr__(self): return "Coord:-- " + str(self.__dict__) def add(a, b): return Coordinate(a.x + b.x, a.y + b.y) def sub(a, b): return Coordinate(a.x - b.x, a.y - b.y) def wrapper(func): def checker(a, b): # 1 if a.x < 0 or a.y < 0: a = Coordinate(a.x if a.x > 0 else 0, a.y if a.y > 0 else 0) if b.x < 0 or b.y < 0: b = Coordinate(b.x if b.x > 0 else 0, b.y if b.y > 0 else 0) ret = func(a, b) if ret.x < 0 or ret.y < 0: ret = Coordinate(ret.x if ret.x > 0 else 0, ret.y if ret.y > 0 else 0) return ret return checker one = Coordinate(100, 200) two = Coordinate(300, 200) three = Coordinate(-100, -100) add = wrapper(add) #sub = wrapper(sub)