How to pass class instance to a method?

Ian Kelly ian.g.kelly at gmail.com
Tue Nov 27 00:07:10 CET 2012


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.



More information about the Python-list mailing list