2014-03-03 73 views
0

我們說我們有一個實例瞭解Python類和基礎類

client = PiHttpClient("192.168.1.234") 
# RPi native GPIO 
gpio = NativeGPIO(client) 
gpio.setFunction(25, "out") 
state = True 

而且從clients.py代碼我有

class PiMixedClient(): 
    def __init__(self, host, port=8000, coap=5683): 
    def sendRequest(self, method, uri): 

class PiHttpClient(PiMixedClient): 
    def __init__(self, host, port=8000): 
     PiMixedClient.__init__(self, host, port, -1) 
class NativeGPIO(GPIO): 
    def __init__(self, client): 
     RESTAPI.__init__(self, client, "/GPIO") 

class GPIO(Device): 
    def __init__(self, client, name): 
     Device.__init__(self, client, name, "digital") 

    def getFunction(self, channel): 
     return self.sendRequest("GET", "/%d/function" % channel) 

    def setFunction(self, channel, func): 
     return self.sendRequest("POST", "/%d/function/%s" % (channel, func)) 

class Device(RESTAPI): 
    def __init__(self, client, name, category): 
     RESTAPI.__init__(self, client, "/devices/" + name + "/" + category) 

class RESTAPI(): 
    def __init__(self, client, path): 
     self.client = client 
     self.path = path 

    def sendRequest(self, method, path): 
     return self.client.sendRequest(method, self.path + path) 
  1. 所以,從上面的時候它PiHttpClient(「192.168.1.234」)主機=「192.168.1.234」,對不對?但是init(self,host,port = 8000)尋找自己,主機。我沒有看到自我被作爲論點傳入。

  2. 然後內部PiMixedClient,因爲PiHttpClient延伸PiMixedClient,那麼它的主機和自我應儘可能相同PiMixedClient

  3. 然後GPIO = NativeGPIO(客戶端)再次INIT NativeGPIO的 _ 內部初始化 (自我,客戶),從調用函數我不需要提供自我?

  4. 所以當擴展到最低級時它變成了RESTAPI基類,它的sendRequest方法來自客戶端,它來自PiMixedClient類的sendRequest?

+0

請更正您的縮進.. – adil

回答

0
  1. 是 - 蟒蛇方法總是能夠自我作爲第一個參數,你可能要採取班看看docs
  2. -
  3. 不是,你如果你創建類(如果你想覆蓋init方法你這樣做)
的實例需要手動suply自我

但是,真的,看看python文檔,它們很不錯。