Conflicting needs for __init__ method

Gabriel Genellina gagsl-py at yahoo.com.ar
Sun Jan 14 22:25:53 EST 2007


At Sunday 14/1/2007 20:32, dickinsm at gmail.com wrote:

>Of course, none of this really has anything to do with rational
>numbers.  There must be many examples of classes for which internal
>calls to __init__, from other methods of the same class, require
>minimal argument processing, while external calls require heavier and
>possibly computationally expensive processing.  What's the usual way
>to solve this sort of problem?

In some cases you can differentiate by the type or number of 
arguments, so __init__ is the only constructor used.
In other cases this can't be done, then you can provide different 
constructors (usually class methods or static methods) with different 
names, of course. See the datetime class, by example. It has many 
constructors (today(), fromtimestamp(), fromordinal()...) all of them 
class methods; it is a C module.

For a slightly different approach, see the TarFile class (this is a 
Python module). It has many constructors (classmethods) like taropen, 
gzopen, etc. but there is a single public constructor, the open() 
classmethod. open() is a factory, dispatching to other constructors 
depending on the combination of arguments used.


-- 
Gabriel Genellina
Softlab SRL 


	

	
		
__________________________________________________ 
Preguntá. Respondé. Descubrí. 
Todo lo que querías saber, y lo que ni imaginabas, 
está en Yahoo! Respuestas (Beta). 
¡Probalo ya! 
http://www.yahoo.com.ar/respuestas 




More information about the Python-list mailing list