[Baypiggies] Class methods as constructors?
charles.merriam at gmail.com
Tue Sep 2 20:28:39 CEST 2008
This may seem like a basic question. Apologies if it's a known recipe.
I have a spot where I need a number of constructors for the same class
and don't want to have too complicated an __init__ function. Using
class methods seems like the cleanest code.
x = Spam() # Simple case
x = Spam.Pickled_Loaf(['Pimentos','Menthos'])
x = Spam.Shake('chunky')
with code in the Spam class like:
def Picked_Loaf(additives, extra_gelatin = False):
assert not ('Menthos' in additives and 'Diet Coke' in additives)
i = Spam() # make an instance
i.additives = additives
Is this the usual pattern? Any gotchas? Usually use "cls()" instead
More information about the Baypiggies