Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #33957

Re: How to pass class instance to a method?

Newsgroups comp.lang.python
Date 2012-11-26 17:14 -0800
References <3193e3dd-0507-4ff7-9026-ee80e5d9c1dd@googlegroups.com>
Message-ID <9ea139f7-7be9-4e39-81e2-06fbef7a2cb8@googlegroups.com> (permalink)
Subject Re: How to pass class instance to a method?
From Drew <r.drew.davis@gmail.com>

Show all headers | View raw


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 "duck typing".  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 sure things 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?

Back to comp.lang.python | Previous | NextPrevious in thread | Find similar | Unroll thread


Thread

How to pass class instance to a method? ALeX inSide <alex.b.inside@gmail.com> - 2012-11-25 04:11 -0800
  Re: How to pass class instance to a method? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-11-25 12:37 +0000
  Re: How to pass class instance to a method? Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2012-11-26 11:22 +1300
  Re: How to pass class instance to a method? Alain Ketterlin <alain@dpt-info.u-strasbg.fr> - 2012-11-26 11:08 +0100
  Re: How to pass class instance to a method? Nobody <nobody@nowhere.com> - 2012-11-26 16:56 +0000
    Re: How to pass class instance to a method? Ian Kelly <ian.g.kelly@gmail.com> - 2012-11-26 13:51 -0700
    Re: How to pass class instance to a method? Dave Angel <d@davea.name> - 2012-11-26 16:58 -0500
      Re: How to pass class instance to a method? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-11-26 22:18 +0000
        Re: How to pass class instance to a method? Dave Angel <d@davea.name> - 2012-11-26 22:14 -0500
          Re: How to pass class instance to a method? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-11-27 04:10 +0000
            Re: How to pass class instance to a method? Roy Smith <roy@panix.com> - 2012-11-26 23:25 -0500
            Re: How to pass class instance to a method? Dave Angel <d@davea.name> - 2012-11-27 00:18 -0500
    Re: How to pass class instance to a method? Ian Kelly <ian.g.kelly@gmail.com> - 2012-11-26 16:07 -0700
      Re: How to pass class instance to a method? Hans Mulder <hansmu@xs4all.nl> - 2012-11-27 01:36 +0100
    Re: How to pass class instance to a method? Dave Angel <d@davea.name> - 2012-11-26 21:50 -0500
  Re: How to pass class instance to a method? Drew <r.drew.davis@gmail.com> - 2012-11-26 17:14 -0800

csiph-web