Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how can I change the attribute value in supercalass from subclass permanently in python

I want to change the value of attribute pen_color in View class permanently when I call changeColor method and the value still permenant unless I call the method again with another color,
when I call changeColor method the pen_color attribute still have the same value and I don’t want such thing
Is there any way to do such thing or not ?

class View: 
    def __init__(self):
        self.pen_color = 'red'
    

class Button(View): 
    def __init__(self):
        pass
        
    def changeColor(self):
        super().__init__()
        
        self.pen_color = 'green'

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

You should move the declaration of pen color into a class attribute rather than an instance attribute and reference it as View.pen_color.

Having done that View.pen_color will be permanent and global, but can be shadowed by self.pen_color in either View or Button instances.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading