The Python Oracle

Is it possible to add an attribute to an imported class in python?

Become part of the top 3% of the developers by applying to Toptal https://topt.al/25cXVn

--

Music by Eric Matyas
https://www.soundimage.org
Track title: Music Box Puzzles

--

Chapters
00:00 Question
03:18 Accepted answer (Score 3)
03:42 Thank you

--

Full question
https://stackoverflow.com/questions/2435...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#python #class #attributes #pygame

#avk47



ACCEPTED ANSWER

Score 3


Yes its possible to add attribute to instances and classes (as its an object too). Just use setattr.

But why do you do that? you should inherit the Rect class and make a ColoredRect

class ColoredRect(Rect):
    def __init__(self, color, ...):
        self.color = color
        super(ColoredRed, self).__init__(self, ...)