Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > How to pass class instance to a method?

Reply
Thread Tools

How to pass class instance to a method?

 
 
Drew
Guest
Posts: n/a
 
      11-27-2012
On Sunday, November 25, 2012 7:11:29 AM UTC-5, ALeX inSide wrote:
> 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


I'm not sure I understand exactly what you sre asking. Python uses "ducktyping". As far as Python is concerned, you can pass in any class object and so long as it has the needed methods, it'll suffice ("If it walks like a duck and it quacks like a duck, then it is a duck."). The down side to that is that if you hand an object as an argument and the method you pass doesn't behave like the expected method class would, then bad things may happen at run time. It would be a bit of a hassle to check types to make surethings at least smell OK before execution goes possibly awry, but you are certainly free to write guard code that makes those sort of checks.

My reply here is a bit different from the other replies I see so far. I worry that may mean I mis-understood your question. Has this been at all helpful an answer?
 
Reply With Quote
 
 
 
 
Dave Angel
Guest
Posts: n/a
 
      11-27-2012
On 11/26/2012 06:07 PM, Ian Kelly wrote:
> On Mon, Nov 26, 2012 at 2:58 PM, Dave Angel <(E-Mail Removed)> 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.


We were talking about function arguments. I don't know of any place
where they get their types declared.

--

DaveA

 
Reply With Quote
 
 
 
 
Dave Angel
Guest
Posts: n/a
 
      11-27-2012
On 11/26/2012 05:18 PM, Steven D'Aprano wrote:
> 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:


That's backwards. Any body should be a bug in that case. It doesn't
matter what you pass to a function that is unspecified, it's behavior is
undefined. Calling it is inherently illegal.

>
> 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/


I read part of it, and it's more than I care to read tonight. It seems
to be written by an anonymous person. By jumping around in his blog, I
see a lot of interesting articles, but i haven't yet figured out who he
is. Does he have a name? A degree, a job in computers, a reputation?





--

DaveA

 
Reply With Quote
 
Steven D'Aprano
Guest
Posts: n/a
 
      11-27-2012
On Mon, 26 Nov 2012 22:14:59 -0500, Dave Angel wrote:

> On 11/26/2012 05:18 PM, Steven D'Aprano wrote:
>> 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:

>
> That's backwards. Any body should be a bug in that case. It doesn't
> matter what you pass to a function that is unspecified, it's behavior is
> undefined. Calling it is inherently illegal.


Have you ever programmed before? *wink*

Seriously, as much as we would like to have full documentation of every
piece of code before it is written, such a thing is awfully heavyweight
for all but the biggest projects.

In practice, many functions never get documented at all, or only
partially documented. Whether this is a good thing or not, it is a fact,
and no mainstream language *requires* you to write documentation, nor is
the documentation is used to determine runtime behaviour. If it did, it
would be code, not documentation.

In lightweight or agile software development methodologies ("Bingo!") or
exploratory development, you often write the code before you know what it
does, or even what you want it to do. E.g. I'll sometimes have a vague
idea of what I want a function or method to do, and go through three or
four iterations of writing code before it is stable enough to begin
documenting it.

Given the practical reality that documentation is often neglected, there
is a school of thought that says that *code* is the One True source of
information about what the code does, that documentation is at best a
hint or at worst completely redundant. While I think that's a bit
extreme, I can see the point. If function f() puts the cat on the mat,
but is documented as putting the hat on the cat, how do you know whether
the documentation is wrong or the code?


[...]
>> Please, everybody, before replying to this thread, please read this:
>>
>> http://cdsmith.wordpress.com/2011/01...ticle-i-wrote/

>
> I read part of it, and it's more than I care to read tonight. It seems
> to be written by an anonymous person. By jumping around in his blog, I
> see a lot of interesting articles, but i haven't yet figured out who he
> is. Does he have a name? A degree, a job in computers, a reputation?


Does it matter? Surely what he says should stand or fail on its own
merits, not by who he is.

He has a name, although it seems to be hard to find on his current blog:
Chris Smith. As for the rest, I don't know.



--
Steven
 
Reply With Quote
 
Roy Smith
Guest
Posts: n/a
 
      11-27-2012
Steven D'Aprano <(E-Mail Removed)> wrote:
> Given the practical reality that documentation is often neglected, there
> is a school of thought that says that *code* is the One True source of
> information about what the code does, that documentation is at best a
> hint or at worst completely redundant.


Yes, there is such a school. Those people are full of bovine excrement.

> If function f() puts the cat on the mat, but is documented as putting
> the hat on the cat, how do you know whether the documentation is
> wrong or the code?


Documentation should describe intent and interface. Yes, the code says
what the code does. But, the interface description says what it's
supposed to do. Can the docs be wrong? Of course they can. Usually
because somebody changed the code and didn't bother to change the docs.

My take on people who never document anything is that they're just plain
lazy.

Go ahead, ask me how I really feel about the topic
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Don't understand behavior; instance form a class in another class'instance Martin P. Hellwig Python 1 03-26-2010 12:06 AM
Problem when subclass instance changes base class instance variable Gerry Sutton Python 1 04-16-2005 06:06 AM
converting base class instance to derived class instance Sridhar R Python 14 02-10-2004 02:47 PM
Cannot refer to an instance member of a class from within a shared method or shared member initializer without an explicit instance of the class. DJ Dev ASP .Net 3 02-08-2004 04:19 PM



Advertisments