Chris Spencer a écrit : > Kalle Anke wrote: > >> On Sun, 12 Jun 2005 13:59:27 +0200, deelan wrote >> (in article <[EMAIL PROTECTED]>): >> >> void doSomething( data : SomeClass ){ ... } >> >> and I would be sure at compile time that I would only get SomeClass >> objects as parameters into the method. > > > Being an untyped language, Python does not require you to enforce types.
Nope. Python *is* typed. But it doesnt confuse implementation with semantic. > However, for those that require such functionality, you can get away > with using the "assert" statement. For example, if I wanted to make sure > my function foo was only given instances of class Bar, I'd write > something like: > > >>> class Bar: pass > >>> def foo(bar): > ... assert isinstance(bar, Bar), "argument is not of type Bar" > ... print "argument must be of type Bar" ... > >>> bar = Bar() > >>> foo(bar) > argument must be of type Bar > >>> foo(123) > Traceback (most recent call last): > File "<stdin>", line 1, in ? > File "<stdin>", line 2, in foo > AssertionError: argument is not of type Bar > >>> And *this* is highly unpythonic. And un-OO too, since it makes foo() dependant on *class* Bar, when it should most probably be enough that it only depends on (probably part of) the *interface* of class Bar. Suppose I want to adapt my own class Baaz so it's compatible with Bar. Now I *must* make the adapter a subclass of Bar (and then depends on Bar's *implementation*) when it only needs to expose the same (probably subset of) interface. Except for exceptions (please pardon the pun), one most usually don't have to worry about the real class of an object. In 5 years programming Python, I only had to check for the type of an object a couple of times, and each time only for more genericity (I mostly needed to dispatch on some 'generic types' like string-like, sequence-like, map-like, numeric-like, callable, and others), and this was never used as a way to restrict the possible types (exceptions are good enough for this). -- http://mail.python.org/mailman/listinfo/python-list