strange behaviour with decorators.

Antoon Pardon apardon at forel.vub.ac.be
Wed Feb 9 05:27:19 EST 2005


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



More information about the Python-list mailing list