Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #98821 > unrolled thread

What is wrong this wrapper (decorator)?

Started byfl <rxjwg98@gmail.com>
First post2015-11-14 09:23 -0800
Last post2015-11-16 19:30 +1300
Articles 3 — 2 participants

Back to article view | Back to comp.lang.python


Contents

  What is wrong this wrapper (decorator)? fl <rxjwg98@gmail.com> - 2015-11-14 09:23 -0800
    Re: What is wrong this wrapper (decorator)? fl <rxjwg98@gmail.com> - 2015-11-14 09:29 -0800
    Re: What is wrong this wrapper (decorator)? Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2015-11-16 19:30 +1300

#98821 — What is wrong this wrapper (decorator)?

Fromfl <rxjwg98@gmail.com>
Date2015-11-14 09:23 -0800
SubjectWhat is wrong this wrapper (decorator)?
Message-ID<dafb679e-c94c-47d2-9209-ce2a86a673d6@googlegroups.com>
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]: <function checker>


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)

[toc] | [next] | [standalone]


#98822

Fromfl <rxjwg98@gmail.com>
Date2015-11-14 09:29 -0800
Message-ID<cbff0d72-c735-4a7f-8f24-a998b3c05c0e@googlegroups.com>
In reply to#98821
On Saturday, November 14, 2015 at 12:23:50 PM UTC-5, fl wrote:
> 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]: <function checker>
> 
> 
> 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)

Excuse me. I just realize that the indent made the logic incorrect.
It is different from other language. Thanks.

[toc] | [prev] | [next] | [standalone]


#98868

FromGregory Ewing <greg.ewing@canterbury.ac.nz>
Date2015-11-16 19:30 +1300
Message-ID<datbh0F80u3U1@mid.individual.net>
In reply to#98821
fl wrote:
> wrapper(sub(two, one))
> Out[38]: <function checker>

This doesn't do what you probably meant it to do.
It first calls sub() with arguments one and two, and
then passes the result of that (a Coordinate, if
sub is working properly) to wrapper(). Since
wrapper() expects a function, not a Coordinate,
that won't do anything useful.

What you probably meant to do is:

wrapper(sub)(one, two)

This passes the function sub to wrapper, which
returns another function; that function is then
called with arguments one and two.

-- 
Greg

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web