0

When building getter and setter methods in Python, I understand you can define attributes with @property and @x.setter or define the methods __getattr__ and __setattr__. Both ways appear the same to the user as in it replaces directly accessing attributes with getter and setter methods. But it seems like with @property you can define getters and setters for each attribute while with __getattr__ and __setattr__ you can only define one getter and setter for the class. So is there any reason to not use @property?

EDIT: Not really sure how those other questions answer mine? The upvoted question answers how to replace using __setattr__ and using it correctly which is not my question. The other question is downvoted and closed for lacking focus, and I was hoping this question improved upon that one.

geckels1
  • 347
  • 1
  • 3
  • 13
  • 3
    Use __getattr__ and __setattr__ typically when you need to dynamically have attributes available. If you know what the name of the attribute will be, property is far more readable and allows static type checkers to function properly. – flakes Mar 30 '23 at 00:49

0 Answers0