Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 23160

Standard convention when a property has to be accessed through its accessor in Python? [closed]

$
0
0

Im asking this because I didn't find any answers that suffice my doubts about Uniform Access Principles in Python, and what is a good practice.

Inside a class, I have the methodology to call or set a property depending on the next cases:

  • For getting I use self.property syntax (and not self._property).
  • For edit, skipping any "check" via the setter, I'll use self._property = something (for example, for initializing the property in the __init__ method).
  • For edit without skipping the inner logic of the setter, I'll use self.property = something.

Is this a correct guideline? If not, what is the standard convention? Why and when should a property be accessed through its accessor?

Assume the next class for the question:

class Foo:    def __init__(self, something):        self._something = something    @property    def something(self) -> Any:        return self._something    @something.setter    def something(self, any_value):        # any validation        self._something = any_value

Viewing all articles
Browse latest Browse all 23160

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>