Python calling method in class

python call class method from instance
python call class method from another class method
python class
python class method
python class __call__ method
how to call a function outside the class in python
python static method
python static class

I'm punching way above my weight here, but please bear with this Python amateur. I'm a PHP developer by trade and I've hardly touched this language before.

What I'm trying to do is call a method in a class...sounds simple enough? I'm utterly baffled about what 'self' refers to, and what is the correct procedure to call such a method inside a class and outside a class.

Could someone explain to me, how to call the move method with the variable RIGHT. I've tried researching this on several 'learn python' sites and searches on StackOverflow, but to no avail. Any help will be appreciated.

The following class works in Scott's Python script which is accessed by a terminal GUI (urwid).

The function I'm working with is a Scott Weston's missile launcher Python script, which I'm trying to hook into a PHP web-server.

class MissileDevice:
  INITA     = (85, 83, 66, 67,  0,  0,  4,  0)
  INITB     = (85, 83, 66, 67,  0, 64,  2,  0)
  CMDFILL   = ( 8,  8,
                0,  0,  0,  0,  0,  0,  0,  0,
                0,  0,  0,  0,  0,  0,  0,  0,
                0,  0,  0,  0,  0,  0,  0,  0,
                0,  0,  0,  0,  0,  0,  0,  0,
                0,  0,  0,  0,  0,  0,  0,  0,
                0,  0,  0,  0,  0,  0,  0,  0,
                0,  0,  0,  0,  0,  0,  0,  0)
  STOP      = ( 0,  0,  0,  0,  0,  0)
  LEFT      = ( 0,  1,  0,  0,  0,  0)
  RIGHT     = ( 0,  0,  1,  0,  0,  0)
  UP        = ( 0,  0,  0,  1,  0,  0)
  DOWN      = ( 0,  0,  0,  0,  1,  0)
  LEFTUP    = ( 0,  1,  0,  1,  0,  0)
  RIGHTUP   = ( 0,  0,  1,  1,  0,  0)
  LEFTDOWN  = ( 0,  1,  0,  0,  1,  0)
  RIGHTDOWN = ( 0,  0,  1,  0,  1,  0)
  FIRE      = ( 0,  0,  0,  0,  0,  1)

  def __init__(self, battery):
    try:
      self.dev=UsbDevice(0x1130, 0x0202, battery)
      self.dev.open()
      self.dev.handle.reset()
    except NoMissilesError, e:
      raise NoMissilesError()

  def move(self, direction):
    self.dev.handle.controlMsg(0x21, 0x09, self.INITA, 0x02, 0x01)
    self.dev.handle.controlMsg(0x21, 0x09, self.INITB, 0x02, 0x01)
    self.dev.handle.controlMsg(0x21, 0x09, direction+self.CMDFILL, 0x02, 0x01)

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

Let's say you have:

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

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

Then, doing:

a = A()
a.foo() #prints 'Foo'
a.bar('Arg!') #prints 'Bar Arg!'

There's nothing special about this being called self, you could do the following:

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

    def bar(this_object):
        this_object.foo()

Then, doing:

b = B()
b.bar() # prints 'Foo'

In your specific case:

dangerous_device = MissileDevice(some_battery)
dangerous_device.move(dangerous_device.RIGHT) 

(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:

dangerous_device.move(RIGHT)

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

Python's Instance, Class, and Static Methods Demystified – Real , Notice how Python automatically passes the class as the first argument to the function when we call MyClass.classmethod() . Calling a method in Python through  Python: Call Parent class method. A class is a user-defined blueprint or prototype from which objects are created. Classes provide a means of bundling data and functionality together. Creating a new class creates a new type of object, allowing new instances of that type to be made.

Let's 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 don't 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.set_attribute3(3)
f.attribute3                         # >>> 3

Python Tutorial: class method vs static method - 2020, How do you call a class method from another class in Python? I also wanted to note that your method calls are going to be problematic. You defined is_prime to take self as its only argument, but when you call it, you're passing in self.number or n. If you need is_prime to work with arbitrary numbers, and not just whatever the class was initialized with, you should add an extra argument.

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

>>> myMissile = MissileDevice(myBattery)  # looks like you need a battery, don't 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 Foo.bar; 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 self.bar
...     bar = 'baz'
...
>>> Foo.quux
<unbound method Foo.quux>
>>> Foo.bar
'baz'
>>> f = Foo()
>>> f.bar
'baz'
>>> 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>
baz
>>>

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.

How to call a method in another class of the same package?, will be automatically converted to a class method function call like this: class. method(instance, args) When we call the method, the Python automatically replace the self with the instance object, a, and then the msg gets the string passed at the call which is 'instance call'. How do I call the printMessage(self) method from another file, example.py in the same directory? I thought this answer was close, but it shows how to call a class method from another class within the same file.

How to call methods in another class in Python, How to call an instance method in the same class in Python. class c: def f(self): print("abc") def g(self): self. f() print("def") Function g( ) calls function f( ) class_instance = c() class_instance. f() The first method __init__()is a special method, which is called class constructor or initialization method that Python calls when you create a new instance of this class. You declare other class methods like normal functions with the exception that the first argument to each method is self.

Python Functions Examples: Call, Indentation, Arguments & Return , The classmethod() method returns a class method for the given function. we call printAge without creating a Person object like we do for static methods. Notice how Python automatically passes the class as the first argument to the function when we call MyClass.classmethod (). Calling a method in Python through the dot syntax triggers this behavior. The self parameter on instance methods works the same way. Please note that naming these parameters self and cls is just a convention.

How to call an instance method in the same class in Python, The first method __init__() is a special method, which is called class constructor or initialization method that Python calls when you create a new instance of this class. You declare other class methods like normal functions with the exception that the first argument to each method is self. Call method from another class in a different class in Python we can call the method of another class by using their class name and function with dot operator.

Comments
  • What do you think is wrong with your code? It looks like you're using self right.
  • Its not about the above code, thats a class taken from working code. Im asking a more elementary question; how to call a method within a class.
  • I was asking how to call a method from a class, using this given example. All as stated above. I was after an explanation rather than a solution.
  • i faced the similar issue, while creating the class. I used the self.<methodName> to call the method inside the class. outSide the class <ObjectName>.<MethodName>. Hope you got the point
  • considering how RIGHT is defined on the class, it was probably the author's intent that it be called ClassName.ATTRIBUTE instead of one_instance.ATTRIBUTE, as though it should be the same for all instances, and you don't need an instance to refer to it; so... MissileDevice.RIGHT is probably the "right" way to use this class.
  • I couldnt get this to work for my specific example, but I believe it is something to do with what 'battery' is, as the question is actually about class/methods which what you explained is correct, Im gona to mark this answer as correct. I thought id note this here, in case someone searching for developed missile.py code comes here.