Duncan Booth wrote:
Mr.SpOOn <[EMAIL PROTECTED]> wrote:
Now I must pass a and b to the main constructor and calculate them in
the classmethods.
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.
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.
One built-in model for .__init__ + .from_data is dict. __init__ can
take either one iterable or several keywords. In either case, it gets a
set of key:value pairs. .from_keys take a set of keys and an optional
value to override None that is matched with all keys.
--
http://mail.python.org/mailman/listinfo/python-list