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)
If you have programmed in any other language with classes, besides python, this sort of thing
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 itFoo.bar
; you can also access class attributes through instances of the class, likeFoo().bar
.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.
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:
2) You want to use the method (or attribute) of a class outside the definition of that class
3) You want to use the method (or attribute) of an instantiated class:
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:
Then, doing:
There's nothing special about this being called
self
, you could do the following:Then, doing:
In your specific case:
(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!