Home > Software design >  Can I extend parent's class list attribute on class level?
Can I extend parent's class list attribute on class level?

Time:09-28

Suppose, I have the following parent class:

class Parent:
    permissions = ['CanEdit', 'CanCreate']

I need to extend permissions attribute in my child class without changing the initial contents and so that this change would be on the class level, not instance, meaning:

print(Parent.permissions)
    

Output:

['CanEdit', 'CanCreate']

And I need the something like this in my child class:

class Child(Parent):
    permissions  = ['CanManage']
        

print(Child.permissions)

With the output:

['CanEdit', 'CanCreate', 'CanManage']

Is this even possible to implement?

CodePudding user response:

You can use permissions = Parent.permissions ['CanManage'].

  • Related