0

I have a class with a bunch of generic variables in the init method not specific to the class. I want to make another class that holds these variables and then have the first class inheritance those variables.

So the second class is not a parent class, but more like a module class.

How I write this in python?

Thanks.

Class A():
   def __init__(self):
       self.generic_variable1 = 'blah'
       self.generic_variable2 = 'blah'
       self.generic_variable3 = 'blah'
       self.generic_variable4 = 'blah'
       self.generic_variable5 = 'blah'
       self.generic_variable6 = 'blah'
   
   def generic_method1(self):
       return 'blah'
   
   def class_specific_method1(self):
       pass

After creating two classes:

Class A():
   def __init__(self):
       pass
   
   def class_specific_method1(self):
       pass

Class B():
   def __init__(self):
       self.generic_variable1 = 'blah'
       self.generic_variable2 = 'blah'
       self.generic_variable3 = 'blah'
       self.generic_variable4 = 'blah'
       self.generic_variable5 = 'blah'
       self.generic_variable6 = 'blah'
   
   def generic_method1(self):
       return 'blah'
   
1
  • 2
    It kind of sounds like you are describing a mixin, but without any code it's hard to know for sure. Commented Jul 5, 2020 at 3:57

2 Answers 2

1

A module level dictionary that the class can access.

d = {'k':'v','q':'r','m':'n',...}

class F:
    variables = d
    def __init__(self,...)

Or maybe a collections.namedtuple instead of a dictionary to give you dot access - instance.variables.attr instead of instance.variables[attr]` .

If you want individual attributes:

d = {'k':'v','q':'r','m':'n'}

class F:
    def __init__(self):
        for attr,val in d.items():
            setattr(self,attr,val)

Or set the attributes outside of the class

d = {'k':'v','q':'r','m':'n'}

class F:
    def __init__(self):
        pass

for k,v in d.items():
    setattr(F,k,v)
Sign up to request clarification or add additional context in comments.

3 Comments

That doesnt sound clean though. Then I have to unpack those variables.
Does what Geroge said works on just inheriting the class? Dont we only inherit if its parent child though right?
@BoPeng - see edit. Using inheritance will accomplish what you want. Whether there has to be a parent/child relationship to use inheritance sounds like a philosophical question there is certainly no technical requirement.
1

Just simply

class A:
    ... ...
    
class B(A):
    ... ...

In class B, if you do not rewrite __init__, it will automatically use class A's.

If you need do extra thing in class B's __init__:

class B(A):
    def __init__(self, *args, **kw):
        super().__init__(*args, **kw)
        ... ...

2 Comments

Hi George, thanks for that. I thought we only inherit if its a parent child relationship. This is more like inheriting a bunch of generic variables that are not specific to the class that I want to create a new class to inherit to make the first class look cleaner. Thanks
@BoPeng 老哥,你也是国人吧,说实话我没搞懂你啥意思,要不你用中文说下 Seems that you know Chinese, would you explain in Chinese again? I don't really understand what you meant.

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.