[Tutor] static methods & class methods
ramit.prasad at jpmorgan.com
Fri Apr 20 18:00:58 CEST 2012
> I have a class, and want to use a method of it in the class itself.
> How can I do it?
> I mean, say I defined foo() , bar() methods in a class myClass. So, how
> can we i use foo() in bar(). I tried to use @staticmethod, @classmethod,
> but I am getting some errors..
> sometimes saying "unbound " and sometimes "given more than 1 parameters"
> .. can any one tell me how to do this...
You should read about object oriented programming in Python.
http://www.alan-g.me.uk/tutor/index.htm is a good starting point (on the
left under 'Advanced Topics').
I take this to mean you want to call a method in the class from another
method in the class. I assume you do not need to use "staticmethod" and
do something like below:
< do something including calling foo >
Every method in a class *must* take in the instance. The convention
is to call this "self". To access another method you use the instance
and call the method desired. An unbound function is a function that
is not tied to an object/class. This is also why you got "given more
than 1 parameters" error. I assume you did not include self in the
function definition and Python was passing it. This becomes the
class A( object ): # I use Python 2.x
def foo( self ): # must include self
def bar( self ):
self.foo() # This uses the instance to call foo
Ramit Prasad | JPMorgan Chase Investment Bank | Currencies Technology
712 Main Street | Houston, TX 77002
work phone: 713 - 216 - 5423
This email is confidential and subject to important disclaimers and
conditions including on offers for the purchase or sale of
securities, accuracy and completeness of information, viruses,
confidentiality, legal privilege, and legal entity disclaimers,
available at http://www.jpmorgan.com/pages/disclosures/email.
More information about the Tutor