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__(c ls, name, bases, *args, **kwargs)
print 'cls=',cls, cls.__new
cls.__new__ = staticmethod(cl s.__new)
def __new(self,cls, *args):
print 'new(',self,cls ,args,')'
return object.__new__( cls,*args)
class A:
__metaclass__ = Metaclass
def __init__(cls,*a rgs):
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__(c ls, name, bases, *args, **kwargs)
print 'cls=',cls, cls.__new
cls.__new__ = staticmethod(st aticmethod(cls. __new))
def __new(cls,*args ):
print 'new(',cls,args ,')'
return object.__new__( cls,*args)
class A:
__metaclass__ = Metaclass
def __init__(cls,*a rgs):
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__(c ls, name, bases, *args, **kwargs)
print 'cls=',cls, cls.__new
cls.__new__ = staticmethod(cl s.__new)
def __new(cls,*args ):
print 'new(',cls,args ,')'
return object.__new__( cls,*args)
__new = staticmethod(__ new)
class A:
__metaclass__ = Metaclass
def __init__(cls,*a rgs):
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__(c ls, 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(st aticmethod(__ne w))
class A:
__metaclass__ = Metaclass
def __init__(cls,*a rgs):
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 0 1608 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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()"
|
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 means that variabls such as x and y appearing inside class statements
in Python essentially behave like static class variables in other languages.
On the other hand a variable is specified as an instance variable as self.x
and self.y inside a class in...
|
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() around the existing code.
So I made a metaclass that essentially replaces every method
of a class with a 'wrapper' method, that does the locking,
invocation, unlocking.
Is this the right approach? It seems to work fine. But I have
|
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
|
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 guess is that the warning is related to PyChecker not
supporting C++ extensions.
| |
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 I need to 'disable' the code in M_B on C ? The correct way
to do that seems to be with a M_C metaclass, subclass of M_B,
implementing but not calling parent class methods, or calling 'type'
methods.
|
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 understand staticmethod objects don't need to implement __call__ for
|
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 M_A (type) :
|
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 custom metaclass with
a __getattribute__ method, the method is used when acessing some
attribute directly with the class object, but not when you do it from
the instance.
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |