[英]Python calling method in class
我在這里超過了我的體重,但請忍受這個Python業余愛好者。 我是一名PHP開發人員,我之前幾乎沒有接觸過這種語言。
我想要做的是在課堂上調用一個方法......聽起來很簡單? 我完全不知道“自我”指的是什么,以及在課堂內和課堂外調用這種方法的正確程序是什么。
有人可以向我解釋 ,如何使用變量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)
所有方法的第一個參數通常稱為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)
但是,這取決於您希望如何組織代碼!
假設你有一個閃亮的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
有人可以向我解釋,如何使用變量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對象上使用一個屬性時,解釋器會注意到,當查找屬性在類上時,它是一個函數,它應該返回一個“綁定”方法而不是函數本身。 所有這一切都安排將實例作為第一個參數傳遞。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.