Flask 源码为什么使用property有些代码不用装饰器方式, 有些又用了?

最近看到flask的某段源码, 看到一个类定义里面, 有些属性使用了装饰器形式的property写法, 有些却用了property实例化的写法, 这是有什么用途呢?实在看不懂为什么?

    def _get_static_url_path(self):
        if self._static_url_path is not None:
            return self._static_url_path

        if self.static_folder is not None:
            return '/' + os.path.basename(self.static_folder)

    def _set_static_url_path(self, value):
        self._static_url_path = value

    static_url_path = property(
        _get_static_url_path, _set_static_url_path,
        doc='The URL prefix that the static route will be registered for.'
    )
    del _get_static_url_path, _set_static_url_path

    @property
    def has_static_folder(self):
        """This is ``True`` if the package bound object's container has a
        folder for static files.

        .. versionadded:: 0.5
        """
        return self.static_folder is not None
阅读 2k
1 个回答

殊途同归~ 确切的说:这俩达到的效果是相同的。

容我猜测一下,我猜这是个人风格的问题(不过,每个人随着时间的推移,风格也会变的)。不信你看~

还有一种情况,就是 当你只需要对某个值获取的时候进行处理的时候,注解的方式是比较简便快捷的~

clipboard.png

推荐问题