473,396 Members | 2,055 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,396 software developers and data experts.

Inconsistency of special class method lookup?

Folks,

I'm running into the following issue. A staticmethod of a class seems
not to be accepted as a special class method of the class object
itself. For example:

class Foo(object):
def __len__(): return 2
__len__ = staticmethod(__len__)
print len(Foo)
Traceback (most recent call last):
File "C:/Dokumente und Einstellungen/All Users/Dokumente/foo.py",
line 4, in ?
print len(Foo)
TypeError: len() of unsized object

However, the following works:

class FooType(type):
def __len__(self): return self.l()
class Foo(object):
__metaclass__ = FooType
def l(): return 3
l = staticmethod(l)
print len(Foo)

3

Any good reason why the lookup process doesn't find __len__ as
staticmethod of the class?

Regards,
Sebastian (posting using the account of my wife)

Mar 11 '06 #1
2 1001
an***********@wangnick.de wrote:
class Foo(object):
********def __len__(): return 2
********__len__ = staticmethod(__len__)
print len(Foo)
Traceback (most recent call last):
**File*"C:/Dokumente*und*Einstellungen/All*Users/Dokumente/foo.py",
line 4, in ?
****print*len(Foo)
TypeError: len() of unsized object

However, the following works:

class FooType(type):
********def __len__(self): return self.l()
class Foo(object):
********__metaclass__ = FooType
********def l(): return 3
********l = staticmethod(l)
print len(Foo)

3

Any good reason why the lookup process doesn't find __len__ as
staticmethod of the class?


Special methods of newstyle objects are always looked up in the class, and
the class of a class is its metaclass. Therefore

len(Foo()) invokes type(Foo()).__len__ which is the same as Foo.__len__

and

len(Foo) invokes type(Foo).__len__ which (in your example) is the same as
FooType.__len__.
Peter
Mar 11 '06 #2
Thanks.

Mar 11 '06 #3

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

Similar topics

46
by: Scott Chapman | last post by:
There seems to be an inconsistency here: Python 2.3.2 (#1, Oct 3 2003, 19:04:58) on linux2 >>> 1 == True True >>> 3 == True False >>> if 1: print "true" ....
11
by: Carlos Ribeiro | last post by:
Hi all, While writing a small program to help other poster at c.l.py, I found a small inconsistency between the handling of keyword parameters of string.split() and the split() method of...
2
by: Peter Clifton | last post by:
Hello all, I am very new to this subject and learning by example. I have a small inconsistency in an XML file when transformed using different processors. I was wondering if someone could shed...
5
by: Joseph Barillari | last post by:
Hi python-list, I've just started using new-style classes and am a bit confused as to why I can't seem to alter methods with special names (__call__, etc.) of new-style class instances. In other...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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:
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.