Reassigning Instance Attributes (Python OOP) | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Reassigning Instance Attributes (Python OOP)

Hello guys, I have a question regarding python OOP. Is it a good practice to change instance/object attributes from code outside of the class? like what I did in this example code. If it's not, then what should we do to change object attributes that have been instantiated? Thank you https://code.sololearn.com/cx8DJdfC2LLe/?ref=app

19th Jun 2020, 9:25 AM
ArthD
ArthD - avatar
4 Answers
+ 2
Thats great! You could also make a color_change method that can change the color to any you type in. Thats a special cat you have there
19th Jun 2020, 9:29 AM
Slick
Slick - avatar
+ 2
Yeah, I also doubt that this is a pythonic way to change instance attributes 😅 Thanks for the answer guys Slick Felipe BF 😁😁
19th Jun 2020, 9:37 AM
ArthD
ArthD - avatar
+ 1
In Python, and as far as I know, changing properties that way is acceptable, though I'm unsure if it's pythonic. I remember you can define getters and setters using decorators. I refer you to this article for more information: https://medium.com/@pranaygore/setters-and-getters-in-python-76b5473b3c83 Regards! :D
19th Jun 2020, 9:33 AM
Felipe BF
+ 1
ArthD Glad to be of help! :D I know Python assumes everybody to be adults and exercise care in doing such changes. That's why I can say for sure it's correct.
19th Jun 2020, 9:39 AM
Felipe BF