On Fri, 14 Sep 2007, Rikard Bosnjakovic wrote: > For me, "if x" would be enough. If you think it's a bad thing when x > is of the wrong data, then you really should check that it contains > *correct* data as well.
That's an okay approach, but, but it's also non-Pythoninc; more of the look-before-you-leap approach rather than ask-forgiveness-not-permission. > Using the the two function of yours, setting x to an integer: > > >>> x = 2 > >>> print test01(x) > Traceback (most recent call last): > File "<stdin>", line 1, in ? > File "/usr/tmp/python-3716vZq", line 3, in test01 > TypeError: unsubscriptable object > >>> print test02(x) > Traceback (most recent call last): > File "<stdin>", line 1, in ? > File "/usr/tmp/python-3716vZq", line 8, in test02 > TypeError: unsubscriptable object which is exactly what I would want it to do: raise an exception on bad data. > Rewriting your test01-function into this: > > def test01(x): > if (type(x) == type((0,)) and > (x is not None) and > (length(x) == 2)): > if x[0]>0: > return x[1]/x[0] > > and testing again: > > >>> x = 2 > >>> print test01(x) > None This silently produces an incorrect result, which is a Bad Thing; and it took a lot more code to do it, too. _______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor