Hardware.gpio_active(True)
這是我的五金類:
import os
import glob
import time
import RPi.GPIO as GPIO
#class to manage hardware -- sensors, pumps, etc
class Hardware(object):
#global vars for sensor
base_dir = '/sys/bus/w1/devices/'
device_folder = glob.glob(base_dir + '28*')[0]
device_file = device_folder + '/w1_slave'
#global var for program
temp_unit = 'F' #temperature unit, choose C for Celcius or F for F for Farenheit
gpio_pin = 17
#function to enable GPIO
@classmethod
def gpio_active(active):
#system params for sensor
if active is True:
os.system('modprobe w1-gpio')
os.system('modprobe w1-therm')
GPIO.setmode(GPIO.BCM)
GPIO.setup(Hardware.gpio_pin, GPIO.OUT)
print 'activating GPIO'
else:
print 'deactivating GPIO'
GPIO.cleanup()
我得到這個錯誤:
TypeError: unbound method gpio_active() must be called with Hardware instance as first argument (got bool instance instead)
我不不想傳遞實例 - 我想gpio_active()
基本上作爲一個函數,但保留對靜態類變量的可訪問性。我認爲這是@classmethod
的目的。我收到與@staticmethod
相同的錯誤。
我誤解了什麼?
不,用'staticmethod'你應該*不會*得到相同的錯誤。這是[staticmethod](https://docs.python.org/2.7/library/functions.html#staticmethod)的描述,它並不隱式地將參數傳遞給函數。 –