Python中的Python调用方法

kir*_*rgy 31 python methods class

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

我想要做的是在课堂上调用一个方法......听起来很简单?我完全不知道"自我"指的是什么,以及在课堂内和课堂外调用这种方法的正确程序是什么.

有人可以向我解释一下如何move用变量调用方法RIGHT.我试过在几个'学习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)
Run Code Online (Sandbox Code Playgroud)

Tho*_*zco 60

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

假设你有:

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

    def bar(self, an_argument):
        print 'Bar', an_argument
Run Code Online (Sandbox Code Playgroud)

然后,做:

a = A()
a.foo() #prints 'Foo'
a.bar('Arg!') #prints 'Bar Arg!'
Run Code Online (Sandbox Code Playgroud)

这个被调用没什么特别的self,你可以做以下事情:

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

    def bar(this_object):
        this_object.foo()
Run Code Online (Sandbox Code Playgroud)

然后,做:

b = B()
b.bar() # prints 'Foo'
Run Code Online (Sandbox Code Playgroud)

在您的具体情况:

dangerous_device = MissileDevice(some_battery)
dangerous_device.move(dangerous_device.RIGHT) 
Run Code Online (Sandbox Code Playgroud)

(正如评论中建议的那样MissileDevice.RIGHT更合适!)

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

dangerous_device.move(RIGHT)
Run Code Online (Sandbox Code Playgroud)

但是,这取决于您希望如何组织代码!

  • 考虑到如何在类上定义`RIGHT`,可能作者的意图是将其称为`ClassName.ATTRIBUTE`而不是`one_instance.ATTRIBUTE`,好像它应该对所有实例都相同,而您不需要一个实例来引用它;所以... `MissileDevice.RIGHT` 可能是使用这个类的“正确”方式。 (2认同)

小智 6

假设你有一个闪亮的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
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)