Python calling method in class

Python calling method in class

The first argument of all methods is usually called self. It refers to the instance for which the method is being called.

Lets say you have:

class A(object):
    def foo(self):
        print Foo

    def bar(self, an_argument):
        print Bar, an_argument

Then, doing:

a = A() #prints Foo!) #prints Bar Arg!

Theres nothing special about this being called self, you could do the following:

class B(object):
    def foo(self):
        print Foo

    def bar(this_object):

Then, doing:

b = B() # prints Foo

In your specific case:

dangerous_device = MissileDevice(some_battery)

(As suggested in comments MissileDevice.RIGHT could be more appropriate here!)

You could declare all your constants at module level though, so you could do:


This, however, is going to depend on how you want your code to be organized!

Lets say you have a shiny Foo class.
Well you have 3 options:

1) You want to use the method (or attribute) of a class inside the definition of that class:

class Foo(object):
    attribute1 = 1                   # class attribute (those dont use self in declaration)
    def __init__(self):
        self.attribute2 = 2          # instance attribute (those are accessible via first
                                     # parameter of the method, usually called self
                                     # which will contain nothing but the instance itself)
    def set_attribute3(self, value): 
        self.attribute3 = value

    def sum_1and2(self):
        return self.attribute1 + self.attribute2

2) You want to use the method (or attribute) of a class outside the definition of that class

def get_legendary_attribute1():
    return Foo.attribute1

def get_legendary_attribute2():
    return Foo.attribute2

def get_legendary_attribute1_from(cls):
    return cls.attribute1

get_legendary_attribute1()           # >>> 1
get_legendary_attribute2()           # >>> AttributeError: type object Foo has no attribute attribute2
get_legendary_attribute1_from(Foo)   # >>> 1

3) You want to use the method (or attribute) of an instantiated class:

f = Foo()
f.attribute1                         # >>> 1
f.attribute2                         # >>> 2
f.attribute3                         # >>> AttributeError: Foo object has no attribute attribute3
f.attribute3                         # >>> 3

Python calling method in class

Could someone explain to me, how to call the move method with the variable RIGHT

>>> myMissile = MissileDevice(myBattery)  # looks like you need a battery, dont know what that is, you figure it out.
>>> myMissile.move(MissileDevice.RIGHT)

If you have programmed in any other language with classes, besides python, this sort of thing

class Foo:
    bar = baz

is probably unfamiliar. In python, the class is a factory for objects, but it is itself an object; and variables defined in its scope are attached to the class, not the instances returned by the class. to refer to bar, above, you can just call it; you can also access class attributes through instances of the class, like Foo().bar.

Im utterly baffled about what self refers too,

>>> class Foo:
...     def quux(self):
...         print self
...         print
...     bar = baz
>>> Foo.quux
<unbound method Foo.quux>
>>> f = Foo()
>>> f
<__main__.Foo instance at 0x0286A058>
>>> f.quux
<bound method Foo.quux of <__main__.Foo instance at 0x0286A058>>
>>> f.quux()
<__main__.Foo instance at 0x0286A058>

When you acecss an attribute on a python object, the interpreter will notice, when the looked up attribute was on the class, and is a function, that it should return a bound method instead of the function itself. All this does is arrange for the instance to be passed as the first argument.

Leave a Reply

Your email address will not be published. Required fields are marked *