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) 4 5835
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
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)
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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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):...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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
|
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...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM)
The start time is equivalent to 19:00 (7PM) in Central...
|
by: erikbower65 |
last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA:
1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
|
by: kcodez |
last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
|
by: Taofi |
last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same
This are my field names
ID, Budgeted, Actual, Status and Differences
...
|
by: DJRhino |
last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer)
If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _
310030356 Or 310030359 Or 310030362 Or...
|
by: lllomh |
last post by:
Define the method first
this.state = {
buttonBackgroundColor: 'green',
isBlinking: false, // A new status is added to identify whether the button is blinking or not
}
autoStart=()=>{
|
by: lllomh |
last post by:
How does React native implement an English player?
|
by: Mushico |
last post by:
How to calculate date of retirement from date of birth
|
by: DJRhino |
last post by:
Was curious if anyone else was having this same issue or not....
I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
| |