How to pass class instance to a method?

Hans Mulder hansmu at xs4all.nl
Mon Nov 26 19:36:04 EST 2012


On 27/11/12 00:07:10, Ian Kelly wrote:
> On Mon, Nov 26, 2012 at 2:58 PM, Dave Angel <d at davea.name> wrote:
>> Not how I would put it.  In a statically typed language, the valid types
>> are directly implied by the function parameter declarations,
> 
> As alluded to in my previous post, not all statically typed languages
> require parameter type declarations to perform static checking.
> 
>> while in a
>> dynamic language, they're defined in the documentation, and only
>> enforced (if at all) by the body of the function.
> 
> That's not even true for Python.  The following example uses Python 2.x:
> 
>>>> class Foo(object):
> ...     def method(self):
> ...         pass
> ...
>>>> Foo.method(4)
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
> TypeError: unbound method method() must be called with Foo instance as
> first argument (got int instance instead)
> 
> That's a run-time check, and it's not enforced by the body of the function.

As Ian already knows, this problem has been fixed in Python 3.

-- HansM




More information about the Python-list mailing list