__new__
EP
EP at zomething.com
Sun Nov 6 02:02:21 EST 2005
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...
More information about the Python-list
mailing list