FAQ

James Stroud <wrote:

The quote implies that when I call carol, b.__init__ should be called.
However, this does not seem to be the case (see code below). What am I
not
understanding? Shouldn't the interpreter call b.__init__ when b is
returned
from carol.__new__?

James

py> class bob(object):
... def __init__(self):
... print self.x
... x = 2
...
py> class carol(object):
... def __new__(cls):
... return b
...
py> b=bob()
py> b.x
2
py> c = carol() # should print "2"
py> c
<__main__.bob object at 0x404333cc>

It seems to produce the output you expected for me (Python 2.4.1 on Windows XP), but this has nothing to do with "carol". How are bob and carol related?

Code:

class bob(object):
def __init__(self):
print self.x
x = 2

class carol(object):
def __new__(cls):
return b
b=bob()
print b.x
c = carol()
c

Output:

>>>
2
2


This code produces the same output:

class bob(object):
def __init__(self):
print self.x
x = 2
## class carol(object):
## def __new__(cls):
## return b
b=bob()
print b.x
## c = carol()
##c


I am interested in the underlying subject but think your code was mispasted into the e-mail...

Search Discussions

Discussion Posts

Previous

Follow ups

Related Discussions

Discussion Navigation
viewthread | post
posts ‹ prev | 2 of 5 | next ›
Discussion Overview
grouppython-list @
categoriespython
postedNov 6, '05 at 6:45a
activeNov 8, '05 at 7:57p
posts5
users5
websitepython.org

People

Translate

site design / logo © 2019 Grokbase