类中的Python调用方法

2024-04-18 12:19:27 发布

您现在位置:Python中文网/ 问答频道 /正文

我的拳击比我的体重还重,但请容忍这个业余的Python。我是一个专业的PHP开发人员,以前几乎没有接触过这种语言。

我想做的是在类中调用一个方法…听起来很简单吗?对于“self”指的是什么,以及在类内和类外调用这种方法的正确过程,我感到非常困惑。

有人能给我解释一下如何用变量RIGHT调用move方法吗。我试过在几个“学习python”网站上研究这个问题,并在StackOverflow上搜索,但没有成功。任何帮助都将不胜感激。

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

我使用的函数是Scott Weston的导弹发射器Python脚本,我正试图将其连接到PHP web服务器。

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)

Tags: 方法devselfright脚本movedefscott
3条回答

所有方法的第一个参数通常称为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) 

(如注释中所建议的,MissileDevice.RIGHT在这里可能更合适!)

但是,您可以在模块级声明所有常量,因此您可以:

dangerous_device.move(RIGHT)

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

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)

如果除了python之外,你还用其他语言编写了类,那么

class Foo:
    bar = "baz"

可能不熟悉。在python中,类是对象的工厂,但它本身就是对象;在其作用域中定义的变量附加到中,而不是类返回的实例。要引用上面的bar,只需调用它Foo.bar;还可以通过类的实例访问类属性,如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
>>>

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

假设你有一个闪亮的Foo类。 你有三个选择:

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

相关问题 更多 >