473,722 Members | 2,346 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Question regarding __new__

Hi,

the behaviour I always observed when creating instances by calling the
class A is that '__init__' is always only called when the object
returned by A.__new__ is an instance of A. This can be observed by the
following code:

class A(object):
def __new__(cls, *args, **kwds):
print "A.__new__" , args, kwds
return object.__new__( B, *args, **kwds)
def __init__(cls, *args, **kwds):
print "A.__init__ ", args, kwds

class B(object):
def __new__(cls, *args, **kwds):
print "B.__new__" , args, kwds
return object.__new__( cls, *args, **kwds)
def __init__(cls, *args, **kwds):
print "B.__init__ ", args, kwds

Interactively A() then gives:
>>A()
A.__new__ () {}
<__main__.B object at 0xb7bed0ec>

Yet [1] says: "[...] some rules for __new__: [...] If you return an
object of a different class, its __init__ method will be called."

Am I missing something? Is this documented somewhere else? Also it
would be nice if someone could point me to the function that implements
this in C. I didn't find anything in object.c or typeobject.c.

Best regards
Frank Benkstein.

[1] http://www.python.org/download/relea...intro/#__new__

--
GPG (Mail): 7093 7A43 CC40 463A 5564 599B 88F6 D625 BE63 866F
GPG (XMPP): 2243 DBBA F234 7C5A 6D71 3983 9F28 4D03 7110 6D51

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (GNU/Linux)

iD8DBQFGAqyuiPb WJb5jhm8RArUoAJ wJJ0Dokhyx0wAWT 9C/oXFIK5ua6ACfb3n A
pnZDNpMzkaHCJ2V 3llTaPFo=
=yX4H
-----END PGP SIGNATURE-----

Mar 22 '07 #1
1 1199
On Mar 22, 10:19 am, Frank Benkstein <frank-pyt...@benkstei n.net>
wrote:
Hi,

the behaviour I always observed when creating instances by calling the
class A is that '__init__' is always only called when the object
returned by A.__new__ is an instance of A. This can be observed by the
following code:

class A(object):
def __new__(cls, *args, **kwds):
print "A.__new__" , args, kwds
return object.__new__( B, *args, **kwds)
def __init__(cls, *args, **kwds):
print "A.__init__ ", args, kwds

class B(object):
def __new__(cls, *args, **kwds):
print "B.__new__" , args, kwds
return object.__new__( cls, *args, **kwds)
def __init__(cls, *args, **kwds):
print "B.__init__ ", args, kwds

Interactively A() then gives:
>A()

A.__new__ () {}
<__main__.B object at 0xb7bed0ec>

Yet [1] says: "[...] some rules for __new__: [...] If you return an
object of a different class, its __init__ method will be called."

Am I missing something? Is this documented somewhere else?
http://docs.python.org/ref/customization.html
Also it
would be nice if someone could point me to the function that implements
this in C. I didn't find anything in object.c or typeobject.c.

Best regards
Frank Benkstein.

[1]http://www.python.org/download/releases/2.2/descrintro/#__new__

--
GPG (Mail): 7093 7A43 CC40 463A 5564 599B 88F6 D625 BE63 866F
GPG (XMPP): 2243 DBBA F234 7C5A 6D71 3983 9F28 4D03 7110 6D51

signature.asc
1KDownload

Mar 22 '07 #2

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

Similar topics

2
1909
by: Stephan Diehl | last post by:
I have a question about metaclasses: How would be the best way to "merge" different metaclasses? Or, more precisely, what is the best way to merge metaclass functionality? The idea is basicly the following: One has several (metaclass) modules that implements an interesting feature. Lets say, I have a metaclass L, that adds logging capabilities to all method calls and another one, called P, that creates properties on the fly.
4
9657
by: T. Kaufmann | last post by:
Hi there, A simple but important question: How can I initialize a super class (like dict) correctly in my subclass constructor? A sample: class MyClass(dict): def __init__(self):
5
1469
by: Todd Johnson | last post by:
Ok, say I have a class MyClass and an __init__(self, a, b) Say that a and b are required to be integers for example. So my init looks like: __init__(self, a, b): try: self.one = int(a) self.two = int(b) except ValueError: #nice error message here
5
2500
by: could ildg | last post by:
As there is already __init__, why need a __new__? What can __new__ give us while __init__ can't? In what situations we should use __new__? And in what situations we must use __new__? Can __new__ take the place of __init__? Thanks.
9
1195
by: Steffen Glückselig | last post by:
Hello, I've been experimenting with metaclasses a bit (even though I am quite a newbie to python) and stumpled over the following problem in my code: class Meta(type): def __init__(cls, name, bases, dct): for attr, value in dct.items(): if callable(value): dct = wrapper(value)
4
1023
by: rzed | last post by:
I'm confused (not for the first time). I create these classes: class T(object): def __new__(self): self.a = 1 class X(T): def __init__(self):
5
7397
by: abcd | last post by:
I wanted to extend tuple but ran into a problem. Here is what I thought would work class MyTuple(tuple): def __init__(self, *args): tuple.__init__(self, args) x = MyTuple(1,2,3,4) That gives me...
5
1469
by: Sandra-24 | last post by:
Ok here's the problem, I'm modifying a 3rd party library (boto) to have more specific exceptions. I want to change S3ResponseError into about 30 more specific errors. Preferably I want to do this by changing as little code as possible. I also want the new exceptions to be a subclass of the old S3ResponseError so as to not break old code that catches it. If I meet these two requirements I expect my modification to make it into boto and then...
10
1522
by: =?iso-8859-1?B?QW5kcuk=?= | last post by:
In my application, I make use of the Borg idiom, invented by Alex Martelli. class Borg(object): '''Borg Idiom, from the Python Cookbook, 2nd Edition, p:273 Derive a class form this; all instances of that class will share the same state, provided that they don't override __new__; otherwise, remember to use Borg.__new__ within the overriden class.
0
8861
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, 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...
0
9236
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 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...
0
9088
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 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...
0
8051
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, 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...
1
6681
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5995
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();...
0
4762
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2601
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2147
bsmnconsultancy
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...

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.