Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > newbie question, what's the difference between built-in functions "__new__" and "__init__"

Reply
Thread Tools

newbie question, what's the difference between built-in functions "__new__" and "__init__"

 
 
chenyu
Guest
Posts: n/a
 
      11-07-2003
Hi,
I have studied other's program and found __new__ and __init__ are
used.

My question is what's the difference between built-in functions
"__new__" and "__init__". Could anyone help me?



Thank you in advance.
kind regards/chenyu
 
Reply With Quote
 
 
 
 
Alex Martelli
Guest
Posts: n/a
 
      11-07-2003
chenyu wrote:

> I have studied other's program and found __new__ and __init__ are
> used.
>
> My question is what's the difference between built-in functions
> "__new__" and "__init__". Could anyone help me?


There are no "builtin functions" by these names. There are two methods
in each type that do have these names. I'll assume your reference to
"built-in functions" is a mistake and explain about the methods, by
showing the equivalent Python code that runs them (most are in fact being
used from C, but with semantics just about as below).


When you call any python object XX, e.g.:
x = XX(some, args)
then Python automatically forwards the call to:
x = type(XX).__call__(XX, some, args)

When XX is a typeobject (including a user-coded class, of the "newstyle"
kind), type(XX) is also known as XX's "metaclass". Unless you deliberately
go out of your way to arrange for a custom metaclass, type(XX) is then the
built-in typeobject also known by the name 'type' (slightly confusing...).

So, basically, when you call a normal typeobject XX to instantiate it, what
happens is that method type.__call__ is called. It does basically:

def __call__(cls, *args, **kwds):
result = cls.__new__(cls, *args, **kwds)
if isinstance(result, cls):
type(result).__init__(result, *args, **kwds)
return result

cls is the typeobject you're calling (instantiating). So, in words:
first of all cls.__new__ is called: its job is to return the object
that will be the result of the typeobject call (instantiation). Of
course, normallly it will build and return a NEW object, and normally
an instance of cls, but that is not strictly mandatory.

IF __new__ does return an instance of cls, then initialization is
completed by calling __init__ on the new instance.

So, this is a classic example of "two-phase initialization". The
job of __new__ is providing a new object with the minimum of work
already done on it that just couldn't be done later (e.g. because
a type is immutable); the job of __init__ is completing a new
object's initialization. As a side, somewhat sneaky effect, __new__
can return pre-existing objects (to "recycle" by reinitialziation)
or even instances of completely unrelated types (to let you call a
typeobject as if it were a factory function instead) -- in the latter
case __init__ isn't automatically called.


Alex


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
FAQ 7.17 What's the difference between dynamic and lexical (static) scoping? Between local() and my()? PerlFAQ Server Perl Misc 0 01-06-2011 05:00 PM
(newbie)Technically what's the difference between memset() andmemcpy() functions? sam C++ 11 09-12-2008 12:47 PM
Difference between bin and obj directories and difference between project references and dll references jakk ASP .Net 4 03-22-2005 09:23 PM
please help me in distinguish redefining functions, overloading functions and overriding functions. Xiangliang Meng C++ 1 06-21-2004 03:11 AM
Exact difference between 'const char *' and 'char *', also diff between 'const' and 'static' Santa C Programming 1 07-17-2003 02:10 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57