Re: More __init__ methods
On Fri, Nov 7, 2008 at 4:16 PM, Duncan Booth
<duncan.booth@i nvalid.invalidw rote:
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.
What if I need the parse method to be called in other parts of the program?
On Fri, Nov 7, 2008 at 4:16 PM, Duncan Booth
<duncan.booth@i nvalid.invalidw rote:
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(n ame): ...
>
'from_string' is a bad name here for your factory method: you should try to
make it clear what sort of string is expected.
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(n ame): ...
>
'from_string' is a bad name here for your factory method: you should try to
make it clear what sort of string is expected.
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.
What if I need the parse method to be called in other parts of the program?
Comment