error: 'staticmethod' object is not callable

M

Michal Vitecek

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,
 
P

Peter Otten

Michal said:
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

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
 
M

Michal Vitecek

Peter said:
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?

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?
 
P

Peter Otten

Michal said:
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?
.... assert callable(m), "staticmethod expects a callable as argument"
.... return __builtin__.staticmethod(m)
........ 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

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
 
M

Michael Hudson

Michal Vitecek said:
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?

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top