Connecting Tech Pros Worldwide Help | Site Map

error: 'staticmethod' object is not callable

Michal Vitecek
Guest
 
Posts: n/a
#1: Jul 18 '05
hello everyone,

today i've come upon a strange exception, consider the following file
test.py:

--- beginning of test.py ---

class A(object):
def method1(parA):
print "in A.method1()"
method1 = staticmethod(method1)

def method2(parA, parB):
print "in A.method2()"
method1 = staticmethod(method1)
# see (*)

A.method1("some value")

--- end of test.py ---

when test.py is run, the following error is printed:

$ python test.py
Traceback (most recent call last):
File "test.py", line 10, in ?
A.method1("some value")
TypeError: 'staticmethod' object is not callable
$

isn't this a bug somewhere in python? this was tested on 2.2.3.

(*) this happened accidentaly by copy & paste error


thank you,
--
fuf (fuf@mageo.cz)

Peter Otten
Guest
 
Posts: n/a
#2: Jul 18 '05

re: error: 'staticmethod' object is not callable


Michal Vitecek wrote:
[color=blue]
> hello everyone,
>
> today i've come upon a strange exception, consider the following file
> test.py:
>
> --- beginning of test.py ---
>
> class A(object):
> def method1(parA):
> print "in A.method1()"
> method1 = staticmethod(method1)
>
> def method2(parA, parB):
> print "in A.method2()"
> method1 = staticmethod(method1)
> # see (*)
>
> A.method1("some value")
>
> --- end of test.py ---
>
> when test.py is run, the following error is printed:
>
> $ python test.py
> Traceback (most recent call last):
> File "test.py", line 10, in ?
> A.method1("some value")
> TypeError: 'staticmethod' object is not callable
> $
>
> isn't this a bug somewhere in python? this was tested on 2.2.3.
>
> (*) this happened accidentaly by copy & paste error[/color]

You are wrapping method2() twice:

class A(object):
def method(parA):
print "in A.method()"
method = staticmethod(staticmethod(method))

A.method("first")

Why would you expect this to work?

Peter

Michal Vitecek
Guest
 
Posts: n/a
#3: Jul 18 '05

re: error: 'staticmethod' object is not callable


Peter Otten wrote:[color=blue]
>You are wrapping method2() twice:
>
>class A(object):
> def method(parA):
> print "in A.method()"
> method = staticmethod(staticmethod(method))
>
>A.method("first")
>
>Why would you expect this to work?[/color]

i don't expect this to work. i just don't get it why python allows this
double wrapping of a method. it cannot be used for anything reasonable,
can it?

--
fuf (fuf@mageo.cz)

Peter Otten
Guest
 
Posts: n/a
#4: Jul 18 '05

re: error: 'staticmethod' object is not callable


Michal Vitecek wrote:
[color=blue]
> Peter Otten wrote:[color=green]
>>You are wrapping method2() twice:
>>
>>class A(object):
>> def method(parA):
>> print "in A.method()"
>> method = staticmethod(staticmethod(method))
>>
>>A.method("first")
>>
>>Why would you expect this to work?[/color]
>
> i don't expect this to work. i just don't get it why python allows this
> double wrapping of a method. it cannot be used for anything reasonable,
> can it?
>[/color]
[color=blue][color=green][color=darkred]
>>> import __builtin__
>>> def staticmethod(m):[/color][/color][/color]
.... assert callable(m), "staticmethod expects a callable as argument"
.... return __builtin__.staticmethod(m)
....[color=blue][color=green][color=darkred]
>>> class A(object):[/color][/color][/color]
.... def method():
.... pass
.... method = staticmethod(method)
.... method = staticmethod(method)
....
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "<stdin>", line 5, in A
File "<stdin>", line 2, in staticmethod
AssertionError: staticmethod expects a callable as argument[color=blue][color=green][color=darkred]
>>>[/color][/color][/color]

Is this what you want then?
Personally, I don't care much, because
(1) I use static methods very rarely.
(2) The error message makes it clear enough that something's wrong with a
static method.

If you think it's important, you could either use a wrapper function like
above in your code or submit a patch.

Peter

Michael Hudson
Guest
 
Posts: n/a
#5: Jul 18 '05

re: error: 'staticmethod' object is not callable


Michal Vitecek <fuf@mageo.cz> writes:
[color=blue]
> Peter Otten wrote:[color=green]
> >You are wrapping method2() twice:
> >
> >class A(object):
> > def method(parA):
> > print "in A.method()"
> > method = staticmethod(staticmethod(method))
> >
> >A.method("first")
> >
> >Why would you expect this to work?[/color]
>
> i don't expect this to work. i just don't get it why python allows this
> double wrapping of a method. it cannot be used for anything reasonable,
> can it?[/color]

Hmm, in Python 2.3 the classmethod constructor checks its argument for
callability, but the staticmethod constructor doesn't seem to. I
guess this is just oversight...

Cheers,
mwh

--
59. In English every word can be verbed. Would that it were so in
our programming languages.
-- Alan Perlis, http://www.cs.yale.edu/homes/perlis-alan/quotes.html
Closed Thread