More __init__ methods

Marc 'BlackJack' Rintsch bj_666 at gmx.net
Fri Nov 7 13:02:11 EST 2008


On Fri, 07 Nov 2008 17:23:21 +0100, Mr.SpOOn wrote:

> On Fri, Nov 7, 2008 at 4:16 PM, Duncan Booth
> <duncan.booth at invalid.invalid> wrote:
>> There is a really big advantage to being explicit in this situation:
>> you no longer have to make sure that all your constructors use a unique
>> set of types. Consider:
>>
>> class Location(object):
>>    def __init__(self, lat, long): ...
>>
>>    @classmethod
>>    def from_city(name): ...
>>
>>    @classmethod
>>    def from_postcode(name): ...
>>
>> 'from_string' is a bad name here for your factory method: you should
>> try to make it clear what sort of string is expected.
> 
> Yes, that's what I was going to do.
> But, for example, I have a parse method to create such object from a
> string. So I need to call this method to actually create the object. Now
> I must put the code of the parse method into the @classmethod
> constructor.

You can still spread it over other `classmethod()`\s or `staticmethod()`s 
or even plain functions at module level.

> What if I need the parse method to be called in other parts of the
> program?

I don't understand!?  Then you call it from those other parts.

Ciao,
	Marc 'BlackJack' Rintsch



More information about the Python-list mailing list