"F. GEIGER" <[EMAIL PROTECTED]> writes: > As Philippe already said, use objects that support the protocol or decide > what to do with it after having checked its type. I do that, if I have to, > like so: > > 1 def doIt(arg): > 2 if type(arg) == type([]): > 3 map(doIt, arg) > 4 else: > 5 # Do it on a scalar type > 6 # ... > 7 return result
Now, consider that line 3 would execute very happily if the type of arg were 1) list, 2) str, 3) tuple, 4) dict, 5) file, 6) any other built-in iterable or sequence, 7) any useer-defined iterable or sequence, 8) a subclass of any of the above, 9) god knows what else ... ... yet in line 2 you have ensured that the whole function will not work properly for any of the listed types other than the first. You could make the code much more general by doing it like this: try: map(doIt, arg) except TypeError: ... The important thing to note is that the actual types of Python objects are usually not very interesting or important at all. What is much more important is what the object is able to do: what messages it understands, what protocols it supports. Hiding some code behind a type-check, in Python, is quite frequently the wrong thing to do. -- http://mail.python.org/mailman/listinfo/python-list