On Jan 24, 3:38 pm, "abcd" <[EMAIL PROTECTED]> wrote: > In my code I am debating whether or not to validate the types of data > being passed to my functions. For example > > def sayHello(self, name): > if not name: > rasie "name can't be null" > if not isinstance(name, str): > raise "name must be a string" > print "Hello " + name > > Is the use of isinstance a "bad" way of doing things? is it a "heavy" > operation? for example, if I use this in each function validate input > will it slow things down a lot? > > just curious how you might handle this type of situation (other than > not validating at all). > > thanks
My opinion is that validation is generally good. However, you have to make it not too strict. For example, instead of print "Hello " + name you could have written print "Hello " + str(name) In this case requirement isinstance() will be too strict. The only thing you have to check is that hasattr(name, "__str__") and callable(name.__str__) In this case you can have validation, while at the same time enjoy full flexibility of dynamic typing. -- Maxim -- http://mail.python.org/mailman/listinfo/python-list