0

I use RaspberryPi3 with Python to Remote control GPIO of other RPIs.

I created a class to initialize connections and pin for all Pis:

  class relay(pigpio.pi):

   def __init__(self,ip_addr):
     pigpio.pi.__init__(self)
     self.GPIO=[4,5,6,12]
     self.rpi=pigpio.pi(ip_addr)
     for t in range(len(self.GPIO)):
         self.rpi.write(self.GPIO[t],0)

   def switch_state(self,i,state):
     self.rpi.write(self.GPIO[i],state)

pi_1=relay('192.168.2.112')   # creating first Rpi link
pi_2=relay('192.168.2.113')   # creating second Rpi link

x=0
pi_1.switch_state(x,0)

how can I inherit pigpio module's attributes into relay ? in order not to create switch_state as I did, but to use read, write and more that belong to pigpio

1
  • You can only inherit from classes, not from modules. Commented Jul 27, 2017 at 9:24

1 Answer 1

2

If I'm right you want to extend a module to a class by inheritance.

If that is true, you can not perform that without hacky things but anyway, you should not do that.

Module and classes are not designed to be used in that way. You better keep using module's functions as expected, I see no good reasons to map module's function within a class.

You could just map it by hand like:

import spam

class Foo():
    def egg(self, *args, **kwargs):
        return spam.egg(*args, **kwargs)

But again, I'm not sure there is a valid reason to do that - but there is plenty of valid reasons to not..

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

Comments

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.