Python Class Method Error — unbound requires instance
我打电话来
1 | Hardware.gpio_active(True) |
这是我的硬件课程:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | 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)
我不想传递一个实例——我希望
我误解了什么?
您可以使用
1 2 3 | @staticmethod def gpio_active(active): ... |
但看起来您应该使用
1 2 3 | @classmethod def gpio_active(cls, active): ... |
然后用
把
更多关于