简体   繁体   中英

How to inherit Class attributes Python

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

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..

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM