473,404 Members | 2,137 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

error: 'staticmethod' object is not callable

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
4 5900
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: IHateSuperman | last post by:
public class StaticField2{ public static void main(String args){ private int x, y; // <<== error 1 for ( y = 0 ; y < 100 ; y++){ x = StaticMethod(); System.out.println(" x = "+x); } } public...
0
by: Robin Becker | last post by:
A colleague wanted to initialize his class __new__ and tried code resembling this #######################1 class Metaclass (type): def __init__(cls, name, bases, *args, **kwargs):...
1
by: Neil Zanella | last post by:
Hello, Coming from C++ and Java, one of the surprising things about Python is that not only class instances (AKA instance objects) but also classes themselves are objects in Python. This...
4
by: Will Stuyvesant | last post by:
Add your funny or surprising Python error messages to this thread. A requirement is that you should also show (minimal) code that produces the message. Put the code below, so people can think...
1
by: homepricemaps | last post by:
if i do the following i get the url of an image i am looking for image = "" image = bs.img print image however if i do this out.write (image ) i get an error that says "nonetype error is...
10
by: Nicolas Fleury | last post by:
Hi everyone, I was wondering if it would make sense to make staticmethod objects callable, so that the following code would work: class A: @staticmethod def foo(): pass bar = foo() I...
0
by: Steven Bethard | last post by:
Steven Bethard wrote: > (For anyone else out there reading who doesn't already know this, > Steven D'Aprano's comments are easily explained by noting that the > __get__ method of staticmethod...
4
by: 7stud | last post by:
Hi, Can someone show me how to manually implement staticmethod()? Here is my latest attempt: ---------------- def smethod(func): def newFunc(): pass
14
by: james_027 | last post by:
hi, python's staticmethod is the equivalent of java staticmethod right? with classmethod, I can call the method without the need for creating an instance right? since the difference between...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.