类中的Python调用方法

Posted

技术标签:

【中文标题】类中的Python调用方法【英文标题】:Python calling method in class 【发布时间】:2012-12-14 17:47:21 【问题描述】:

我在这里的出拳远超我的体重,但请耐心等待这位 Python 业余爱好者。我是一名 php 开发人员,以前几乎没有接触过这种语言。

我想要做的是在一个类中调用一个方法......听起来很简单吗?我对“自我”指的是什么以及在类内和类外调用这种方法的正确程序是什么感到完全困惑。

有人可以向我解释,如何使用变量RIGHT 调用move 方法。我已经尝试在几个“学习 python”网站上研究这个并在 *** 上搜索,但无济于事。任何帮助将不胜感激。

以下类在 Scott 的 Python 脚本中工作,该脚本由终端 GUI (urwid) 访问。

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

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 对。 与上面的代码无关,那是从工作代码中提取的一个类。我问了一个更基本的问题;如何在类中调用方法。 我在问如何从一个类中调用一个方法,使用这个给定的例子。一切如前所述。我是在寻求解释而不是解决方案。 我在创建课程时遇到了类似的问题。我使用 self. 来调用类中的方法。在类 . 之外。希望你明白了 【参考方案1】:

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

假设你有:

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 并没有什么特别之处,您可以执行以下操作:

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

    def bar(this_object):
        this_object.foo()

然后,做:

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

在您的具体情况下:

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

(正如 cmets 中所建议的那样,MissileDevice.RIGHT 在这里可能更合适!)

可以在模块级别声明所有常量,因此您可以这样做:

dangerous_device.move(RIGHT)

不过,这将取决于您希望如何组织代码!

【讨论】:

考虑到 RIGHT 是如何在类上定义的,作者的意图可能是它被称为 ClassName.ATTRIBUTE 而不是 one_instance.ATTRIBUTE,就好像它应该对所有实例都一样,并且您不需要实例来引用它;所以...MissileDevice.RIGHT 可能是使用此类的“正确”方式。 我无法让它适用于我的具体示例,但我相信这与“电池”是什么有关,因为问题实际上是关于您解释正确的类/方法,我要将此答案标记为正确。我想在这里注意一下,以防有人在这里搜索开发的 missile.py 代码。【参考方案2】:

谁能给我解释一下,如何使用变量 RIGHT 调用 move 方法

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

如果你用任何其他语言编写了类,除了python,这种事情

class Foo:
    bar = "baz"

可能不熟悉。在python中,类是对象的工厂,但它本身就是对象;并且在其范围内定义的变量附加到,而不是类返回的实例。参考上面的bar,可以直接叫Foo.bar;您还可以通过类的实例访问类属性,例如Foo().bar


我也完全不知道“自我”指的是什么,

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

当你访问 python 对象的属性时,解释器会注意到,当查找的属性在类上并且是一个函数时,它应该返回一个“绑定”方法而不是函数本身。所有这一切都是安排实例作为第一个参数传递。

【讨论】:

【参考方案3】:

假设你有一个闪亮的 Foo 类。 那么你有 3 个选择:

1) 您想在该类的定义中使用该类的方法(或属性):

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) 您想在该类定义之外使用该类的方法(或属性)

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)你想使用实例化类的方法(或属性):

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调用方法的主要内容,如果未能解决你的问题,请参考以下文章

梳理:python—同一个类中的方法调用

Python 3 - 调用方法来更改类中的另一个方法

python 装饰器调用其他类中的方法

如何python3中调用python2中的方法

xposed 如何主动调用java层类中的方

请教,父类中如何调用子类中的虚函数