Generic Python

Chris Liechti cliechti at gmx.net
Mon Jun 24 15:03:30 EDT 2002


"John Roth" <johnroth at ameritech.net> wrote in 
news:uhe1069gs1f4fc at news.supernews.com:

> 
> "Uwe Mayer" <Uwe.Mayer at ifib.uni-karlsruhe.de> wrote in message
> news:3D16E465.F0D2509E at ifib.uni-karlsruhe.de...
>> Hi,
>>
>> is it possible to to write a class which, f.e. takes an argument in
>> __init__() and that doesn't return an instance object but a new class
>> object?
> 
> Sure, but you have to do it closer to the source level. One
> quibble here: you can't do it with the __init__() method, that's
> defined to always return an instance. You have to do it with some
> kind of generator or factory function.

just to clarify: __init__ is _not_ return anything.
1. an instance is created, you can already call methods on it.
2. __init__ is called if defined. (it's an initializer, notthing more or 
less)

that why it isn't called __new__ [hint, hint]

>>> class A(type):
... 	def __new__(klass, arg):
... 		return int(arg)
... 
>>> A(5)
5
>>> type(A(5))
<type 'int'>


chris


-- 
Chris <cliechti at gmx.net>




More information about the Python-list mailing list