The Python Oracle

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

--------------------------------------------------
Hire the world's top talent on demand or became one of them at Toptal: https://topt.al/25cXVn
and get $2,000 discount on your first invoice
--------------------------------------------------

Music by Eric Matyas
https://www.soundimage.org
Track title: Fantascape Looping

--

Chapters
00:00 Is It Possible To Add An Attribute To An Imported Class In Python?
02:46 Accepted Answer Score 3
03:04 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, ...)