Home > OS >  Multiple different roles to a python class
Multiple different roles to a python class

Time:03-23

What is the best way to apply multiple different "roles" to an object of a class.

Let's say I have the following classes

class BasicAccessSettings:
    def __init__(self):
        self.option_1 = True
        self.option_2 = True
        self.option_3 = False 
        self.option_4 = False
        self.option_5 = False
class SpecialAccessSettings:
    def __init__(self):
        self.option_1 = True
        self.option_2 = True
        self.option_3 = True
        self.option_4 = True
        self.option_5 = True

Then I would like to be able to make certain additional modifications to objects of those classes. For example, I could have multiple different settings that modify some of the options, such as something like: SpecialRole1 that has options 1 and 2 set to False or SpecialRole2 that has options 1 and 4 set to True.

Then if I apply SpecialRole1 and SpecialRole2 to BasicAccessSettings (in that order), the resulting settings would be

option_1 = True   # from SpecialRole2
option_2 = False  # from SpecialRole1
option_3 = False  # from BasicAccessSettings
option_4 = True   # from SpecialRole2
option_5 = False  # from BasicAccessSettings

Similarly, I would like to be able to add the same special roles to SpecialAccessSettings if needed. How could this be implemented?

I've considered decorators and inheritance, but wasn't really able to figure out the best way. Inheritance would result in a massive amount of different classes but maybe multiple inheritance could be the solution somehow?

CodePudding user response:

You can try:

class BasicAccessSettings:
    def __init__(self):
        self.option_1 = True
        self.option_2 = True
        self.option_3 = False 
        self.option_4 = False
        self.option_5 = False

    def SpecialRole1(self):
        self.option_1 = False
        self.option_2 = False

    def SpecialRole2(self):
        self.option_1 = True
        self.option_4 = True

    def get_all_options(self):
        print(self.option_1)
        print(self.option_2)
        print(self.option_3)
        print(self.option_4)
        print(self.option_5)


        
obj = BasicAccessSettings()
obj.SpecialRole1()
obj.SpecialRole2()
obj.get_all_options()

output:

True
False
False
True
False

CodePudding user response:

Inheritance works well here:

class Roles:
    def SpecialRole1(self):
        self.option_2 = False
        self.option_2 = False
        
    def SpecialRole2(self):
        self.option_1 = True
        self.option_4 = True

    def __repr__(self):
        return f"Options: {self.option_2} {self.option_2} {self.option_3} {self.option_4} {self.option_5}"

 
class BasicAccessSettings(Roles):
    option_1 = True
    option_2 = True
    option_3 = False
    option_4 = False
    option_5 = False
  
class SpecialAccessSettings(Roles):
    option_1 = True
    option_2 = True
    option_3 = True
    option_4 = True
    option_5 = True

Then you can:

user1 = BasicAccessSettings()
user1.SpecialRole1()
user1.SpecialRole2()
print(user1)

Which will work as expected giving, the default basic plus the role modifications in order:

Options: True False False True False
  • Related