strange behaviour with decorators.

Discussion in 'Python' started by Antoon Pardon, Feb 9, 2005.

  1. I tried the following program:

    def positive(f):

    def call(self, u):

    if u < 1:
    print 'Not Positive'
    raise ValueError
    return f(self, u)

    return call

    class Incrementor:

    def __init__(self, val=0):
    self.value = val

    @positive
    def __call__(self, term = 1):
    print 'incrementing'
    self.value += term
    return self.value

    inc = Incrementor(0)
    try:
    print inc(1)
    print inc()
    print inc(3)
    print inc(-2)
    except:
    pass


    And it gave me the following result:

    incrementing
    1


    Now although this behaviour was surprising after somethought
    I think I may understand why things go wrong, but I certainly
    don't understand the result I got. I would think an error like:

    TypeError: call() takes exactly 2 arguments (1 given)

    would have been more appropiate.


    Am I missing something?
    Is it a bug?
    Maybe both?

    --
    Antoon Pardon
    Antoon Pardon, Feb 9, 2005
    #1
    1. Advertising

  2. > Now although this behaviour was surprising after somethought
    > I think I may understand why things go wrong, but I certainly
    > don't understand the result I got. I would think an error like:
    >
    > TypeError: call() takes exactly 2 arguments (1 given)
    >
    > would have been more appropiate.
    >
    >
    > Am I missing something?
    > Is it a bug?
    > Maybe both?


    Maybe I'm missing something - but I exactly get that error if I don't use
    that try: except: of yours:

    For this

    -----------------
    def positive(f):
    def call(self, u):

    if u < 1:
    print 'Not Positive'
    raise ValueError
    return f(self, u)

    return call

    class Incrementor:
    def __init__(self, val=0):
    self.value = val

    @positive
    def __call__(self, term = 1):
    print 'incrementing'
    self.value += term
    return self.value

    inc = Incrementor(0)

    print inc(1)
    print inc()
    print inc(3)

    --------------------

    I get this result:
    deets@kumquat:~$ python2.4 /tmp/test.py
    incrementing
    1
    Traceback (most recent call last):
    File "/tmp/test.py", line 24, in ?
    print inc()
    TypeError: call() takes exactly 2 arguments (1 give

    --
    Regards,

    Diez B. Roggisch
    Diez B. Roggisch, Feb 9, 2005
    #2
    1. Advertising

  3. Op 2005-02-09, Diez B. Roggisch schreef <>:
    >> Now although this behaviour was surprising after somethought
    >> I think I may understand why things go wrong, but I certainly
    >> don't understand the result I got. I would think an error like:
    >>
    >> TypeError: call() takes exactly 2 arguments (1 given)
    >>
    >> would have been more appropiate.
    >>
    >>
    >> Am I missing something?
    >> Is it a bug?
    >> Maybe both?

    >
    > Maybe I'm missing something - but I exactly get that error if I don't use
    > that try: except: of yours:


    Ah, yes, the penny dropped. The try: except where there because
    originally there were other statements I wanted to test and I
    didn't want the raise exception by the inc(-2) stop the script.
    I completely forget to consider it would also catch the
    error I was expecting.

    Thanks.

    --
    Antoon Pardon
    Antoon Pardon, Feb 9, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Antonio

    Strange encoding behaviour

    Antonio, Dec 29, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    412
    Antonio
    Dec 29, 2004
  2. Arien Malec

    PEP 318 decorators are not Decorators

    Arien Malec, Aug 13, 2004, in forum: Python
    Replies:
    11
    Views:
    551
    Arien Malec
    Aug 16, 2004
  3. Delaney, Timothy C (Timothy)

    RE: strange behaviour with decorators.

    Delaney, Timothy C (Timothy), Feb 9, 2005, in forum: Python
    Replies:
    1
    Views:
    298
    Antoon Pardon
    Feb 10, 2005
  4. Andy Chambers
    Replies:
    1
    Views:
    368
    Daniel Dyer
    May 14, 2007
  5. Francesco Bochicchio

    Unexpected behaviour of inner functions/ decorators

    Francesco Bochicchio, Jun 30, 2009, in forum: Python
    Replies:
    1
    Views:
    209
    David Stanek
    Jun 30, 2009
Loading...

Share This Page