473,378 Members | 1,592 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,378 software developers and data experts.

Odd behavior with staticmethods

I'm getting rather inconsistent behavior with staticmethod.
@staticmethod has the same problems, but I'm demonstrating it with
staticmethod() because it shows things more clearly
---------------------------------------------------
class A: def orig():
print "hi"
st = staticmethod(orig)
st2 = st
wrapped = [ orig ]
wrapped2 = [ st ]

A.orig() # NORMAL - unbound method, should fail
Traceback (most recent call last):
File "<pyshell#9>", line 1, in -toplevel-
A.orig()
TypeError: unbound method orig() must be called with A instance as
first argument (got nothing instead) A.st() # NORMAL - staticmethod, all good hi A.st2() # NORMAL - copy of a static method, all good hi A.wrapped[0]() # ODD - wrapping orig() in a list makes orig() work? hi A.wrapped2[0]() # ODD - copying st works -- A.st2() -- but copying it to a list fails?
Traceback (most recent call last):
File "<pyshell#13>", line 1, in -toplevel-
A.wrapped2[0]()
TypeError: 'staticmethod' object is not callable a = [ A.orig] # NORMAL - fails as expected
a[0]()
Traceback (most recent call last):
File "<pyshell#27>", line 1, in -toplevel-
a[0]()
TypeError: unbound method orig() must be called with A instance as
first argument (got nothing instead) b = [ A.st ] # NORMAL - suceeds as expected
b[0]() hi
type(A.orig) <type 'instancemethod'> type(A.st) <type 'function'> type(A.st2) <type 'function'> type(A.wrapped[0]) <type 'function'> type(A.wrapped2[0]) <type 'staticmethod'> type(a[0]) <type 'instancemethod'> type(b[0])

<type 'function'>

------------------------------------------------------------

So if the class definition is not finished, one can copy static methods
just fine. But if it finds its way into a container (in this case, a
list, but making it a member variable of a child object also breaks),
it gets an extra staticmethod wrapper, or something similar? Once the
class is done, this behavoir no longer occurs.

Can anyone please explain to me why this happens?

Jul 1 '06 #1
6 1276
cm************@yaho.com wrote:
I'm getting rather inconsistent behavior with staticmethod.
Not really.
>>>class A:
def orig():
print "hi"
st = staticmethod(orig)
st2 = st
wrapped = [ orig ]
wrapped2 = [ st ]
....
>>>A.wrapped[0]() # ODD - wrapping orig() in a list makes orig() work?
hi
>>>A.wrapped2[0]() # ODD - copying st works -- A.st2() -- but copying
it to a list fails?
....

When you put orig in wrapped, it is _before_ the class is built,
so you are placing a simple function of no args in a list.

When you put st in wrapped2, it is also _before_ the class is built,
so you are placing staticmethod(a simple function of no args) in a list.

You really should be using new-style classes, some things just
work wrong in "classic classes" (this is not one of those things,
however).

When the class gets built, the staticmethod wrapper is used to determine
how to build A. The class construction gets handed the "namespace"
that was defined in the class suite, and it fumbles over the defined
names determining what conversions to apply (and looking for things
like "__metaclass__" and "__slots__"). For more on all of this read
the language reference, esp. the "Data Model" section, and probably that
on the difference between old-style and new-style classes.

--Scott David Daniels
sc***********@acm.org
Jul 1 '06 #2
ya know, I've searched for these "new classes" at least five times.
I've heard all the wonderful things about how they make your life into
a piece of chocolate with rainbows sprinkled in it. Never once have I
found a site that explains what syntax to use to make these new
classes.

Anyone have a URL?
Scott David Daniels wrote:
cm************@yaho.com wrote:
I'm getting rather inconsistent behavior with staticmethod.
Not really.
>>class A:
def orig():
print "hi"
st = staticmethod(orig)
st2 = st
wrapped = [ orig ]
wrapped2 = [ st ]
...
>>>A.wrapped[0]() # ODD - wrapping orig() in a list makes orig() work?
hi
>>>A.wrapped2[0]() # ODD - copying st works -- A.st2() -- but copying
it to a list fails?
...

When you put orig in wrapped, it is _before_ the class is built,
so you are placing a simple function of no args in a list.

When you put st in wrapped2, it is also _before_ the class is built,
so you are placing staticmethod(a simple function of no args) in a list.

You really should be using new-style classes, some things just
work wrong in "classic classes" (this is not one of those things,
however).

When the class gets built, the staticmethod wrapper is used to determine
how to build A. The class construction gets handed the "namespace"
that was defined in the class suite, and it fumbles over the defined
names determining what conversions to apply (and looking for things
like "__metaclass__" and "__slots__"). For more on all of this read
the language reference, esp. the "Data Model" section, and probably that
on the difference between old-style and new-style classes.

--Scott David Daniels
sc***********@acm.org
Jul 1 '06 #3
cm************@yaho.com schrieb:
ya know, I've searched for these "new classes" at least five times.
I've heard all the wonderful things about how they make your life into
a piece of chocolate with rainbows sprinkled in it. Never once have I
found a site that explains what syntax to use to make these new
classes.
I doubt that.

google: new style classes python

second match:

http://www.geocities.com/foetsch/pyt...le_classes.htm
Happy reading.

Diez
Jul 1 '06 #4
cm************@yaho.com wrote:
ya know, I've searched for these "new classes" at least five times.
I've heard all the wonderful things about how they make your life into
a piece of chocolate with rainbows sprinkled in it. Never once have I
found a site that explains what syntax to use to make these new
classes.
http://www.python.org/doc/newstyle.html

From the docs for the upcoming 2.5:

http://docs.python.org/dev/ref/node33.html
As to how you make them:

class Whatever:
...
is an old-style class.

class Whenever(object):
...
is a new-style class.

class Whoever(SomeClass):
...
is an old-style class if SomeClass is an old-style class,
and a new-style class if SomeClass is a new-style class.

--Scott David Daniels
sc***********@acm.org
Jul 2 '06 #5
THANK YOU!

Now I can actually worry about the advantages/disadvantages!

Jul 2 '06 #6
cm************@yaho.com wrote:
<ot>please stop top-posting, it's getting very annoying</ot>
ya know, I've searched for these "new classes" at least five times.
Then go and buy yourself a pair of glasses. It's one of the entrie in
the "documentation" menu of python.org.
--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom.gro'.split('@')])"
Jul 4 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Paul Morrow | last post by:
Probably the best-loved features of Python are the shortcuts. The fact that you don't have to explicitly declare very much and that the language supports clear, shorthand syntax for frequently...
19
by: E. Robert Tisdale | last post by:
In the context of the comp.lang.c newsgroup, the term "undefined behavior" actually refers to behavior not defined by the ANSI/ISO C 9 standard. Specifically, it is *not* true that "anything can...
23
by: Ken Turkowski | last post by:
The construct (void*)(((long)ptr + 3) & ~3) worked well until now to enforce alignment of the pointer to long boundaries. However, now VC++ warns about it, undoubtedly to help things work on 64...
7
by: Mike Livenspargar | last post by:
We have an application converted from v1.1 Framework to v2.0. The executable references a class library which in turn has a web reference. The web reference 'URL Behavior' is set to dynamic. We...
2
by: Ramashish Baranwal | last post by:
Hi, I want to access a static variable in a staticmethod. The variable can be redefined by derived classes and that should be reflected in base's staticmethod. Consider this trivial example- ...
12
by: Rajesh S R | last post by:
Can anyone tell me what is the difference between undefined behavior and unspecified behavior? Though I've read what is given about them, in ISO standards, I'm still not able to get the...
28
by: v4vijayakumar | last post by:
#include <string> #include <iostream> using namespace std; int main() { string str; str.resize(5); str = 't';
4
by: crazychimp132 | last post by:
Greetings. I am looking for a way to achieve method behavior for a class I created. That is, it has a __call__ method, so can be called like a function. But I also want it to be treated as a...
33
by: coolguyaroundyou | last post by:
Will the following statement invoke undefined behavior : a^=b,b^=a,a^=b ; given that a and b are of int-type ?? Be cautious, I have not written a^=b^=a^=b ; which, of course, is undefined....
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...

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.