Gmail Calendar Documents Reader Web more »
Recently Visited Groups | Help | Sign in
Google Groups Home
__new__
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  5 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
James Stroud  
View profile  
 More options Nov 6 2005, 1:45 am
Newsgroups: comp.lang.python
From: James Stroud <jstr...@mbi.ucla.edu>
Date: Sat, 5 Nov 2005 22:45:37 -0800
Local: Sun, Nov 6 2005 1:45 am
Subject: __new__
Hello All,

I'm running 2.3.4

I was reading the documentation for classes & types
       http://www.python.org/2.2.3/descrintro.html
And stumbled on this paragraph:

"""
__new__ must return an object. There's nothing that requires that it return a
new object that is an instance of its class argument, although that is the
convention. If you return an existing object, the constructor call will still
call its __init__ method. If you return an object of a different class, its
__init__ method will be called.
"""

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>

--
James Stroud
UCLA-DOE Institute for Genomics and Proteomics
Box 951570
Los Angeles, CA 90095

http://www.jamesstroud.com/


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
EP  
View profile  
 More options Nov 6 2005, 2:02 am
Newsgroups: comp.lang.python
From: "EP" <E...@zomething.com>
Date: Sat, 5 Nov 2005 23:02:21 -0800
Local: Sun, Nov 6 2005 2:02 am
Subject: Re: __new__
James Stroud <wrote:

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...


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Peter Otten  
View profile  
 More options Nov 6 2005, 5:03 am
Newsgroups: comp.lang.python
From: Peter Otten <__pete...@web.de>
Date: Sun, 06 Nov 2005 11:03:18 +0100
Local: Sun, Nov 6 2005 5:03 am
Subject: Re: __new__

Here's what "Python in a Nutshell" (p84) says:

"""
Each new-style class has a static method named __new__. When you call
C(*args, **kwds) to create a new instance of a new-style class C, Python
invokes C.__new__(C, *args, **kwds). Python uses __new__'s return value x
as the newly created instance. Then, Python calls C.__init__(x, *args,
**kwds), but only when x is indeed an instance of C (otherwise, x's state
is as __new__ had left it). Thus, for a new-style class C, the statement
x=C(23) is equivlent to the following code:

    x = C.__new__(C, 23)
    if isinstance(x, C): C.__init__(x, 23)
"""

If the following code says what I think it does

class A(object):
    def __init__(self):
        print "init A"
    def __new__(cls):
        return b

class B(A):
    def __init__(self):
        print "init B"

b = object.__new__(B)

print "---"
A()              # prints 'init B'
A.__init__(b)    # prints 'init A'
print "---"
b = 42
A()              # prints nothing

the "Nutshell" example should be changed to

    x = C.__new__(C, 23)
    if isinstance(x, C): x.__init__(23)

to comply with the current implementation (I used Python 2.4).

Peter


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Alex Martelli  
View profile  
 More options Nov 6 2005, 11:12 am
Newsgroups: comp.lang.python
From: al...@mail.comcast.net (Alex Martelli)
Date: Sun, 6 Nov 2005 08:12:19 -0800
Local: Sun, Nov 6 2005 11:12 am
Subject: Re: __new__
Peter Otten <__pete...@web.de> wrote:

   ...

> is as __new__ had left it). Thus, for a new-style class C, the statement
> x=C(23) is equivlent to the following code:

>     x = C.__new__(C, 23)
>     if isinstance(x, C): C.__init__(x, 23)
   ...
> the "Nutshell" example should be changed to

>     x = C.__new__(C, 23)
>     if isinstance(x, C): x.__init__(23)

> to comply with the current implementation (I used Python 2.4).

Hmmm -- not quite, because in the new-style object model special methods
are taken from the type, NOT from the instance as the latter is saying.
E.g, if you change class B into:

class B(A):
    def __init__(self):
        print "init B"
        def f(): print 'from instance'
        self.__init__ = f

you'll see that while b.__init__() would print 'from instance',
instantiating A will not.  I think the right correction to the Nutshell
is therefore:

     x = C.__new__(C, 23)
     if isinstance(x, C): type(x).__init__(x, 23)

and this is how I plan to have it in the 2nd edition.

Alex


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Steven Bethard  
View profile  
 More options Nov 8 2005, 2:57 pm
Newsgroups: comp.lang.python
From: Steven Bethard <steven.beth...@gmail.com>
Date: Tue, 08 Nov 2005 12:57:37 -0700
Local: Tues, Nov 8 2005 2:57 pm
Subject: Re: __new__

Any reason why you're looking at 2.2 documentation when you're running 2.3?

Anyway, the current docs corrected this mistake[1]

"""
If __new__() returns an instance of cls, then the new instance's
__init__() method will be invoked like "__init__(self[, ...])", where
self is the new instance and the remaining arguments are the same as
were passed to __new__().

If __new__() does not return an instance of cls, then the new instance's
__init__() method will not be invoked.
"""

[1]http://docs.python.org/ref/customization.html

STeVe


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2009 Google