2012-12-29 39 views
21

我在這裏衝出重量超過我的體重,但請耐心等待這位Python業餘愛好者。我是一名貿易的PHP開發人員,之前我幾乎沒有碰過這種語言。類中的Python調用方法

我想要做的是調用一個類的方法......聽起來很簡單嗎?我對「自我」是指什麼是完全困惑的,在課堂內部和課堂之外調用這種方法的正確過程是什麼。

是否有人向我解釋如何使用變量RIGHT調用move方法。我已經嘗試過對幾個'學習python'網站和StackOverflow上的搜索進行研究,但無濟於事。任何幫助將不勝感激。

以下類在Scott的Python腳本中工作,該腳本由終端GUI(urwid)訪問。

我正在使用的功能是斯科特韋斯頓的導彈啓動器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) 
+0

你認爲你的代碼有什麼問題?看起來你正在使用「自我」權利。 – Marcin

+0

它不是關於上面的代碼,這是從工作代碼採取的類。我在問一個更基本的問題;如何在類中調用方法。 – kirgy

+0

我在問如何使用這個給定的例子從一個類中調用一個方法。如上所述。我是在解釋而不是解決之後。 – kirgy

回答

43

所有方法的第一個參數通常被稱爲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' 

在您的具體情況:(!正如意見提出MissileDevice.RIGHT可能是更合適的位置)

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

可以聲明在模塊級的所有常量儘管如此,你可以這樣做:

dangerous_device.move(RIGHT) 

但是,這將取決於您希望如何組織代碼!

+1

考慮瞭如何在類上定義RIGHT,它可能是作者的意圖,它被稱爲'ClassName.ATTRIBUTE'而不是'one_instance.ATTRIBUTE',好像它應該對所有實例都一樣,你不需要一個實例來引用它;所以......'MissileDevice.RIGHT'可能是使用這個類的「正確」方式。 – SingleNegationElimination

+0

我不能得到這個爲我的具體例子工作,但我相信這是與什麼'電池'有關,因爲問題實際上是關於類/方法,你解釋的是正確的,我想要標記這個答案爲正確。我認爲id在這裏註明,以防有人在這裏搜索開發的missile.py代碼。 – kirgy

1

有人能向我解釋,如何調用Move方法與變量RIGHT

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

如果您在使用類的任何其他語言進行編程,除了蟒蛇,這樣的事情

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

當你acecss Python對象的一個​​屬性,解釋會注意到,當擡頭屬性是在類上,它是一個函數,它應該返回一個「綁定」方法而不是函數本身。所有這些都是安排實例作爲第一個參數傳遞。

4

比方說,你有一個閃亮的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