Larry Bates <[EMAIL PROTECTED]> writes: > Is there some reason not to use something like the following? > > class foo: > def __init__(self, val): > self.a = 0 > self.b = 0 > > if isinstance(val, basestring): > # > # do something to calculate "a and b" > # > > elif isintance(val, int): > # > # do something else to calculate "a and b" > # > > elif isinstance(val, someinstance) > # > # do something else to calculate "a and b" > # > > else: > # > # Don't know what to do with unknown type > # > raise ValueError()
Yes, the main reason is that it kills duck typing. The initialiser should *use* the parameters passed, and allow exceptions to propagate back to the caller if the parameters don't behave as expected. Another good reason to avoid the above style is that it's far too complex. Different behaviours should be in different functions. -- \ “Friendship is born at that moment when one person says to | `\ another, ‘What! You too? I thought I was the only one!’” —C.S. | _o__) Lewis | Ben Finney -- http://mail.python.org/mailman/listinfo/python-list