Python property with public getter and private setter

后端 未结 2 1317
清歌不尽
清歌不尽 2021-02-09 13:52

I have a python property like this:

class Foo:

    @property
    def maxInputs(self):
        return self._persistentMaxInputs.value

    @maxInputs.setter
             


        
2条回答
  •  栀梦
    栀梦 (楼主)
    2021-02-09 14:28

    I use two properties in a case where I have a public property with private setter. It does create some redundant code, but I end up following the convention with decorators. See example below:

    @property
    def current_dir(self) -> str:
        """
        Gets current directory, analogous to `pwd`
        :return: Current working directory
        """
        return self._current_dir
    
    @property
    def _current_dir(self) -> None:
        return self._current_dir
    
    @_current_dir.setter
    def _current_dir(self, path:str) -> None:
        self._current_dir = path
    

提交回复
热议问题