strange behaviour with decorators.

Diez B. Roggisch deetsNOSPAM at web.de
Wed Feb 9 05:45:27 EST 2005


> 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 at 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



More information about the Python-list mailing list