How to replace constructor with factory method

Chris Rebert clp2 at rebertia.com
Mon May 11 17:08:13 EDT 2009


On Mon, May 11, 2009 at 1:39 PM,  <rogeeff at gmail.com> wrote:
> Hi,
>
> Just wonder if it's possible in Python.
>
> what I want is to tweak an existing Python class A with no
> constructor, so that A() results in fuctory method call?
>
> so o = A() instead being equivalent to:
>
> s = object()
> A.__init__(s)
> o = s
>
> becomes:
>
> o = my_factory_function( A )

I believe you'd need to override __new__ for that.
Docs on __new__: http://docs.python.org/reference/datamodel.html#object.__new__

Cheers,
Chris
-- 
http://blog.rebertia.com



More information about the Python-list mailing list