[Tutor] operators
alan.gauld@bt.com
alan.gauld@bt.com
Fri Mar 28 13:21:01 2003
> muck around with somebody elses C++ code, and its confusing
C++ is always confusing, but if its someone elses then
doubly so! :-)
> heck. So just to re_iterate, if I want a function (class
> method) to do different things based on what is given to
> it, I need to check the type of the objects within the fuction.
There are two possibilities. One is to ensure that all possible
types being passed in support the same interface and only have
one function - the ideal solution. The other one is to heck
type on entry.
The first approach is best but not always practical, the second
messy but works - beware of sequence issues, eg:
def f(a,b):
if type(a) == type(1): return a
elif type(a) == type(b): return b
else: return 42
print f(12,'foo') # -> 12
print f('see','far') # -> 'far'
print f('foo',12) # -> 42!
print f(12,15) # -> 12, but is that what we wanted?
It depends on if/elif order in the typecheck
Alan G