Python中的调用方法 [英] Python calling method in class

查看:144
本文介绍了Python中的调用方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我试图做的是调用一个类中的方法...听起来很简单?对于什么是自我指的也完全不知道,什么是正确的过程在类和类外调用这样的方法。



有人可以向我解释,如何使用变量RIGHT调用move方法。我试过研究这个在几个学习python网站和搜索stackoverflow,但没有效果。任何帮助将不胜感激。



以下类在Scott的python脚本中工作,可以通过终端gui(urwid)访问。



Im工作的函数是Scott Weston的导弹发射器python脚本,我试图挂钩到PHP web服务器。

  class MissileDevice:
INITA =(85,83,66,67,0,0,4,0)
INITB = 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,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()
,除了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)


解决方案

所有方法的第一个参数通常被称为 self 。它指的是被调用方法的实例。



假设您有:

  
def foo(self):
print'Foo'

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

然后,执行:

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






没有什么特别的事情叫 self ,您可以执行以下操作:

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

def bar(this_object):
this_object.foo()

然后,执行:

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






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

(如注释 MissileDevice.RIGHT 中的建议可能更适合! / p>

可以在模块级别声明所有的常量,所以你可以这样做:

  danger_device.move(RIGHT)

将取决于你希望如何组织你的代码!


Im punching way above my weight here, but please bare with this python amateur...im a PHP developer by trade and Ive hardly touched this language before.

What Im trying to do is call a method in a class...sounds simple enough? Im utterly baffled about what 'self' refers too, 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 Im working with is a Scott Weston's missile launcher python script, which Im 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中的调用方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆