More __init__ methods

Ben Finney bignose+hates-spam at benfinney.id.au
Thu Nov 6 15:49:34 EST 2008


Mr.SpOOn <mr.spoon21 at gmail.com> writes:

> class foo:
>     def __init__(self, a, b):
>          self.a = a
>          self.b = b
> 
>     @classmethod
>     def from_string(self, ..):
>           ...
>           ...
> 
> What I mean is: I can't use anymore __init__ as the default
> constructor, but I always have to specify the way I'm creating my
> object. Am I right? I'm asking just to be sure I have understood.

Yes, that's what the above design requires. You code ‘foo.__init__’
for the common use case, and create ‘foo.from_bar’ classmethod factory
functions for other cases. The caller then needs to specify which case
is being requested.

-- 
 \     “I was in the first submarine. Instead of a periscope, they had |
  `\               a kaleidoscope. ‘We're surrounded.’” —Steven Wright |
_o__)                                                                  |
Ben Finney



More information about the Python-list mailing list