Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   How to pass class instance to a method? (http://www.velocityreviews.com/forums/t954824-how-to-pass-class-instance-to-a-method.html)

ALeX inSide 11-25-2012 12:11 PM

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

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.

Pseudo-python-code example:

i = MyClass()

xxx(i, 1, 2);

....
def xxx(self, MyClass myclass, number, foobar):
myclass.classsmethod() #myclass - is an instance of known class

Steven D'Aprano 11-25-2012 12:37 PM

Re: How to pass class instance to a method?
 
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?


Please explain what you mean by this.

What do you think "statically type" means?


> I suppose there shall be some kind of method decorator to treat an
> argument as an instance of class?



Python does not allow you to lie about the type of an argument. All
objects are strongly typed. If an object is a Spam instance, it is a Spam
instance, you can't pretend that it is a Cheese instance.[1]



> Generally it is needed so IDE (PyCharm) can auto-complete instance's
> methods and properties.
>
> Pseudo-python-code example:
>
> i = MyClass()
>
> xxx(i, 1, 2);
>
> ...
> def xxx(self, MyClass myclass, number, foobar):
> myclass.classsmethod() #myclass - is an instance of known class



I do not understand what you are saying here. Please try to explain more
carefully.






[1] You cannot lie about the type of an instance, but sometimes you can
actually change its type. Use this feature with care, since it rarely is
useful.



--
Steven

Gregory Ewing 11-25-2012 10:22 PM

Re: How to pass class instance to a method?
 
ALeX inSide wrote:
> I suppose there shall be some kind of method decorator to treat an argument as an
> instance of class?


You can do this:

xxx = MyClass.some_method

and then

i = MyClass()
xxx(i, foo, bar)

Does that help?

--
Greg

Alain Ketterlin 11-26-2012 10:08 AM

Re: How to pass class instance to a method?
 
ALeX inSide <alex.b.inside@gmail.com> writes:

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


Python does not do static typing.

> I suppose there shall be some kind of method decorator to treat an
> argument as an instance of class?


Decorators are an option. Another is the use of the new parameter
annotations (param : expr) in function/method parameters. (That's python
3, not 2).

> Generally it is needed so IDE (PyCharm) can auto-complete instance's
> methods and properties.


You can't expect static info on the class of the object referenced by
any name, unless you impose strong conventions on the code.

> Pseudo-python-code example:
>
> i = MyClass()
>
> xxx(i, 1, 2);
>
> ...
> def xxx(self, MyClass myclass, number, foobar):
> myclass.classsmethod() #myclass - is an instance of known class


Could: xxx(self,myclass : MyClass, ...)

-- Alain.

Nobody 11-26-2012 04:56 PM

Re: How to pass class instance to a method?
 
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.


Ian Kelly 11-26-2012 08:51 PM

Re: How to pass class instance to a method?
 
On Mon, Nov 26, 2012 at 9:56 AM, Nobody <nobody@nowhere.com> wrote:
> 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.


Not how I would put it. In a statically typed language, types are
checked at compile-time (which does not necessarily imply that useful
type information can be made available to an IDE), whereas in a
dynamically typed language, some or all type checking is deferred to
run-time.

The description that "the set of acceptable types for an argument is
determined by the operations which the function performs on it" sounds
to me more like type inference, as exemplified by Haskell, which is
nonetheless a statically typed language.

Dave Angel 11-26-2012 09:58 PM

Re: How to pass class instance to a method?
 
On 11/26/2012 03:51 PM, Ian Kelly wrote:
> On Mon, Nov 26, 2012 at 9:56 AM, Nobody <nobody@nowhere.com> wrote:
>> 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.

> Not how I would put it. In a statically typed language, types are
> checked at compile-time (which does not necessarily imply that useful
> type information can be made available to an IDE), whereas in a
> dynamically typed language, some or all type checking is deferred to
> run-time.


Not how I would put it. In a statically typed language, the valid types
are directly implied by the function parameter declarations, while in a
dynamic language, they're defined in the documentation, and only
enforced (if at all) by the body of the function.


--

DaveA


Steven D'Aprano 11-26-2012 10:18 PM

Re: How to pass class instance to a method?
 
On Mon, 26 Nov 2012 16:58:47 -0500, Dave Angel wrote:

> In a statically typed language, the valid types
> are directly implied by the function parameter declarations, while in a
> dynamic language, they're defined in the documentation, and only
> enforced (if at all) by the body of the function.



Well that certainly can't be true, because you can write functions
without *any* documentation at all, and hence no defined type
restrictions that could be enforced:

def trivial_example(x):
return x+1

No documentation, and so by your definition above this should be weakly
typed and operate on any type at all. Since there are no type
restrictions defined, the body cannot enforce those type restrictions.
But that's clearly not true.

Please, everybody, before replying to this thread, please read this:

http://cdsmith.wordpress.com/2011/01...ticle-i-wrote/



--
Steven

Ian Kelly 11-26-2012 11:07 PM

Re: How to pass class instance to a method?
 
On Mon, Nov 26, 2012 at 2:58 PM, Dave Angel <d@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.

Hans Mulder 11-27-2012 12:36 AM

Re: How to pass class instance to a method?
 
On 27/11/12 00:07:10, Ian Kelly wrote:
> On Mon, Nov 26, 2012 at 2:58 PM, Dave Angel <d@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



All times are GMT. The time now is 10:06 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.