By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,907 Members | 2,039 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,907 IT Pros & Developers. It's quick & easy.

error: 'staticmethod' object is not callable

P: n/a
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 (fu*@mageo.cz)

Jul 18 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Michal Vitecek wrote:
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

Jul 18 '05 #2

P: n/a
Peter Otten wrote:
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?

--
fuf (fu*@mageo.cz)

Jul 18 '05 #3

P: n/a
Michal Vitecek wrote:
Peter Otten wrote:
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?

import __builtin__
def staticmethod(m): .... assert callable(m), "staticmethod expects a callable as argument"
.... return __builtin__.staticmethod(m)
.... class A(object): .... 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


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

Jul 18 '05 #4

P: n/a
Michal Vitecek <fu*@mageo.cz> writes:
Peter Otten wrote:
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?


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
Jul 18 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.