On 18/06/14 01:15, Nanohard wrote:
On 2014-06-17 13:35, Alan Gauld wrote:

Don't test types, use the interface

Can you please explain what you mean by this?

He means use the Python interpreter, by going to your console and typing 
"python", or in Windows
it's called 'IDLE'.


Nope, I meant what Mark and Danny said.

For example don't do this:

def add(a,b):
    if type(a) == int and type(b) == int:
       return a+b
    else:
       raise TypeError

Just do this:

def add(a,b):
    return a+b

And rely on the interpreter to check that a and b can be added.
It makes the function much more flexible and reusable.
You can now add strings, lists, floats etc, as well as ints


--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.flickr.com/photos/alangauldphotos

_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor

Reply via email to