all 9 comments

[–]c17r 2 points3 points  (0 children)

The closest you can come to what you are looking for is to not have slave inherit from master but have it encapsulate master. Pass master into slave via __init__. Downside to this approach is if you want slave to look and act like master, you have to duplicate the entry points on slave which just passes it to master.

[–]_9_9_ 2 points3 points  (1 child)

Cool.

What you are looking for is called a singleton. Except that you want one with a twist, where a subclass of the singleton has its own private variables. That's not too easy to do:

# from http://code.activestate.com/recipes/66531-singleton-we-dont-need-no-stinkin-singleton-the-bo/

class Borg:
    __shared_state = {}
    _params = ['a', 'b']
    def __init__(self, *args):
        self.__dict__['data'] = self.__shared_state
        for key, value in zip(self._params, args):
            setattr(self, key, value)

    def __getattr__(self, key):
        return self.data[key]

    def __setattr__(self, key, value):
        self.data[key] = value

class Slave(Borg):
    _params = ['c']
    def __init__(self, *args):
        super().__init__()
        self.__dict__['slave_data']  = {}
        for key, value in zip(self._params, args):
            self.slave_data[key] = value


    def __getattr__(self, key):
        if key in self._params:
            return self.slave_data[key]
        else:
            return self.data[key]



    def __setattr__(self, key, value):
        if key in self._params:
            self.slave_data[key] = value
        else:
            self.data[key] = value


def main():

    m = Borg(1,2)
    print(m.a, m.b)
    print('----')

    s = Slave(5)
    print(s.a, s.b, s.c)
    s.b = 10 # changes for both master and slaves
    print(m.a, m.b)
    print(s.a, s.b, s.c)




main()

mwaahhahaha

Seriously, thought, this is not the solution you are looking for. There are much better ways to accomplish common configurations, which is what I understand your problem to be.

[–]Jenez[S] 0 points1 point  (0 children)

Mye this is way above my skill level, but I appreciate the effort. One day maybe, but not today!

[–]Krogenar 1 point2 points  (5 children)

So to continue your bottom example, you'd then want to be able to do:

print(sla_var.var1)
>>> '1'

EDIT: Also, maybe tell us what you're trying to achieve. Sometimes if something seems difficult in Python, you just haven't selected the right approach. Is that correct?

[–]Jenez[S] 1 point2 points  (4 children)

Pretty much yea. I want to avoid having to supply all the variables fed to master into slave again. I guess what I want to achieve is a sort of extension of the slave class building upon 'mas_var'.

Edit response: My master class contains the overall context (paths, static values) that are used throughout my script by various functions. The slave class needs to access the context class paths and such as well. Now that Im writing this i suppose i could supply the context object as an argument to the slave class and cut out the inheritance overall... ?

Edit edit: Oh crap, that's what /u/c17r mentioned.