0

I'm calling

Hardware.gpio_active(True)

This is my Hardware class:

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

I get this error:

TypeError: unbound method gpio_active() must be called with Hardware instance as first argument (got bool instance instead)

I don't want to pass an instance -- I want gpio_active() to basically act as a function but retain accessibility to static class variables. I thought this is what @classmethod was for. I get the same error with @staticmethod.

What am I misunderstanding?

1
  • 1
    No, with staticmethod you should not be getting the same error. That's the description of staticmethod it does not implicitly pass an argument to a function. Commented Jul 27, 2016 at 3:14

2 Answers 2

1

Just replace def gpio_active(active) to def gpio_active(cls, active).

Read more about @classmethod here: https://julien.danjou.info/blog/2013/guide-python-static-class-abstract-methods

Sign up to request clarification or add additional context in comments.

2 Comments

Tried this earlier, same issue.
If you still get TypeError ... got bool instance instead, I guess that you may forget to save your code before run it.
1

You can use a staticmethod:

@staticmethod
def gpio_active(active):
    ... 

But it looks like you should be using a classmethod so you have access to other static/class methods for that class, or access to the class-level variables:

@classmethod
def gpio_active(cls, active):
    ... 

Then replace Hardware.gpio_pin with cls.gpio_pin

2 Comments

I need access to other class methods & variables. Either way I try I get the same error : - /
You can't be getting that error if you're doing the above. Are you sure your stacktrace isn't different in each case?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.