How to pass class instance to a method?

Nobody nobody at nowhere.com
Mon Nov 26 11:56:15 EST 2012


On Sun, 25 Nov 2012 04:11:29 -0800, ALeX inSide wrote:

> How to "statically type" an instance of class that I pass to a method of
> other instance?

Python isn't statically typed. You can explicitly check for a specific
type with e.g.:

	if not isinstance(arg, SomeType):
	    raise TypeError('expected SomeType but got %s' % type(arg))

But this defeats duck typing. If you do this a lot, you're using the wrong
language.

> I suppose there shall be some kind of method decorator to treat an
> argument as an instance of class?
> 
> Generally it is needed so IDE (PyCharm) can auto-complete instance's
> methods and properties.

You have it backwards.

In a dynamically-typed language such as Python, the set of acceptable
types for an argument is determined by the operations which the function
performs on it. This is in direct contrast to a statically-typed language,
where the set of acceptable operations on an argument is determined by the
type of the argument.




More information about the Python-list mailing list