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

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

问题描述

我在这里出拳超过了我的体重,但请容忍这个 Python 业余爱好者.我是一名 PHP 开发人员,以前几乎没有接触过这种语言.

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.

谁能解释给我,如何使用变量RIGHT调用move方法.我试过在几个学习 python"网站上研究这个并在 StackOverflow 上搜索,但无济于事.任何帮助将不胜感激.

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.

以下类在 Scott 的 Python 脚本中运行,该脚本可通过终端 GUI (urwid) 访问.

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

我正在使用的函数是 Scott Weston 的导弹发射器 Python 脚本,我正在尝试将其挂接到 PHP 网络服务器.

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)

推荐答案

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

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

假设您有:

class A(object):
    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 并没有什么特别之处,你可以执行以下操作:


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

然后,做:

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

<小时>

在您的具体情况下:


In your specific case:

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

(正如评论中所建议的,MissileDevice.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天全站免登陆