Guideline for attributes #52
TeresiaOlsson
started this conversation in
Ideas
Replies: 1 comment 2 replies
-
|
I think your suggestion is alright. I only slightly disagree on get and set. I don't mind so much having get/set functions instead of properties, even if it is not so Pythonic. Maybe the guidelines should be added to CONTRIBUTING.md or to some other visible place. |
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
When going through the code I found different ways of how attributes are implemented and I also wasn't entirely sure what to go for myself when doing changes. So I thought deciding on a guideline would be good.
This is my suggestion:
For attributes that are meant to be public to users or other classes and where no special logic is needed for getting/settings, use regular attributes, e.g.
self.x.For attributes that are intended to be private to a class and never called by another class use single underscore, e.g.
self._x.'If another class calls such an attribute, it means the implementation is wrong and should be modified.
For attributes that need special logic for getting/setting, use single underscore + property decorator for getter/setter. Only make functions like get_something and set_something if there is a specific use case for it.
Never use double underscore unless you have a specific use case where it is necessary. This is because name mangling make the code more difficult to understand and debug for non-expert Python programmers.
Beta Was this translation helpful? Give feedback.
All reactions