Why doesn't this work?

Ron Garret rNOSPAMon at flownet.com
Sat Oct 21 13:31:25 EDT 2006


In article <453A507E.4070602 at websafe.com>,
 Larry Bates <larry.bates at websafe.com> wrote:

> Because datetime is a new-style class:

Ah.

> The Constructor __new__
> 
> If you are like me, then you probably always thought of the __init__ method 
> as
> the Python equivalent of what is called a constructor in C++. This isn't the
> whole story.
> 
> When an instance of a class is created, Python first calls the __new__ method 
> of
> the class. __new__ is a static method that is called with the class as its 
> first
> argument. __new__ returns a new instance of the class.
> 
> The __init__ method is called afterwards to initialize the instance. In some
> situations (think "unplickling"!), no initialization is performed. Also,
> immutable types like int and str are completely constructed by the __new__
> method; their __init__ method does nothing. This way, it is impossible to
> circumvent immutability by explicitly calling the __init__ method after
> construction.
> 
> 
> I think what you wanted was:
> 
> >>> class ts(datetime):
> ... 	def __new__(self): pass
> ... 	
> >>> a=ts()
> 
> -Larry

Thanks!

rg



More information about the Python-list mailing list