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

staticmethod vs metaclass

A colleague wanted to initialize his class __new__ and tried code resembling this

#######################1
class Metaclass (type):
def __init__(cls, name, bases, *args, **kwargs):
super(Metaclass, cls).__init__(cls, name, bases, *args, **kwargs)
print 'cls=',cls, cls.__new
cls.__new__ = staticmethod(cls.__new)

def __new(self,cls,*args):
print 'new(',self,cls,args,')'
return object.__new__(cls,*args)

class A:
__metaclass__ = Metaclass
def __init__(cls,*args):
print '__init__(',cls,args,')'
A(3,4,5)
#######################
cls= <class '__main__.A'> <bound method Metaclass.__new of <class '__main__.A'>>
new( <class '__main__.A'> <class '__main__.A'> (3, 4, 5) )
__init__( <__main__.A object at 0x008D0AB0> (3, 4, 5) )
#######################

We wanted to eliminate the extra self argument in the constructed class __new__
so tried
#######################2
class Metaclass (type):
def __init__(cls, name, bases, *args, **kwargs):
super(Metaclass, cls).__init__(cls, name, bases, *args, **kwargs)
print 'cls=',cls, cls.__new
cls.__new__ = staticmethod(staticmethod(cls.__new))

def __new(cls,*args):
print 'new(',cls,args,')'
return object.__new__(cls,*args)

class A:
__metaclass__ = Metaclass
def __init__(cls,*args):
print '__init__(',cls,args,')'
A(3,4,5)
#######################
cls= <class '__main__.A'> <bound method Metaclass.__new of <class '__main__.A'>>
Traceback (most recent call last):
File "mmm.py", line 15, in ?
A(3,4,5)
TypeError: 'staticmethod' object is not callable
#######################

A bit puzzling, but after thought we found both the following do what we want
with the __new__ argument signature.
#######################3
class Metaclass (type):
def __init__(cls, name, bases, *args, **kwargs):
super(Metaclass, cls).__init__(cls, name, bases, *args, **kwargs)
print 'cls=',cls, cls.__new
cls.__new__ = staticmethod(cls.__new)

def __new(cls,*args):
print 'new(',cls,args,')'
return object.__new__(cls,*args)
__new = staticmethod(__new)

class A:
__metaclass__ = Metaclass
def __init__(cls,*args):
print '__init__(',cls,args,')'
A(3,4,5)
#######################
cls= <class '__main__.A'> <function __new at 0x008D60F0>
new( <class '__main__.A'> (3, 4, 5) )
__init__( <__main__.A object at 0x008D0C10> (3, 4, 5) )
#######################

#######################4
class Metaclass (type):
def __init__(cls, name, bases, *args, **kwargs):
super(Metaclass, cls).__init__(cls, name, bases, *args, **kwargs)
print 'cls=',cls, cls.__new
cls.__new__ = cls.__new

def __new(cls,*args):
print 'new(',cls,args,')'
return object.__new__(cls,*args)
__new = staticmethod(staticmethod(__new))

class A:
__metaclass__ = Metaclass
def __init__(cls,*args):
print '__init__(',cls,args,')'

A(3,4,5)
#######################
cls= <class '__main__.A'> <staticmethod object at 0x008D0B90>
new( <class '__main__.A'> (3, 4, 5) )
__init__( <__main__.A object at 0x008D0D90> (3, 4, 5) )
#######################

This does what we want in terms of the signature, but we aren't calling the
correct super __new__, any ideas how we can do that?
--
Robin Becker
Jul 18 '05 #1
0 1588

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

Similar topics

4
by: Michal Vitecek | last post by:
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()"
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...
5
by: Irmen de Jong | last post by:
Hi, I've developed the Metaclass below, because I needed a way to make a bunch of classes thread-safe. I didn't want to change every method of the class by adding lock.aqcuire()..lock.release()...
33
by: Jacek Generowicz | last post by:
I would like to write a metaclass which would allow me to overload names in the definition of its instances, like this class Foo(object): __metaclass__ = OverloadingClass att = 1 att = 3
1
by: Olaf Meding | last post by:
What does the below PyChecker warning mean? More importantly, is there a way to suppress it? PyChecker warning: ..\src\phaseid\integration.py:21: self is argument in staticmethod My best...
14
by: Pedro Werneck | last post by:
Hi I have a class A, with metaclass M_A, and class B, subclass of A, with metaclass M_B, subclass of M_A. A class C, subclass of B must have M_B or a subclass of it as metaclass, but what if...
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...
9
by: Christian Eder | last post by:
Hi, I think I have discovered a problem in context of metaclasses and multiple inheritance in python 2.4, which I could finally reduce to a simple example: Look at following code: class...
4
by: Pedro Werneck | last post by:
Hi all I noticed something strange here while explaining decorators to someone. Not any real use code, but I think it's worth mentioning. When I access a class attribute, on a class with a...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
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: 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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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...

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.