Why does the Flask source code use property? some of the code is not decorated, and some are used again?

recently I saw a piece of source code in flask, and I saw that in a class definition, some attributes were written in the form of property decorator, while others were instantiated in property. What"s the use of this? I really don"t understand why?

    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
Mar.22,2021

different ways lead to the same goal. To be exact, the effect of the two is the same.

Let me guess, I guess it's a matter of personal style (but everyone's style changes over time). If you don't believe it, look ~

there is another situation, that is, when you only need to process a certain value, the way of annotations is relatively simple and fast.

clipboard.png

Menu