美文网首页
python 属性property

python 属性property

作者: SkTj | 来源:发表于2019-12-03 13:46 被阅读0次

    class Person:
    def init(self, first_name):
    self.first_name = first_name

    # Getter function
    @property
    def first_name(self):
        return self._first_name
    
    # Setter function
    @first_name.setter
    def first_name(self, value):
        if not isinstance(value, str):
            raise TypeError('Expected a string')
        self._first_name = value
    
    # Deleter function (optional)
    @first_name.deleter
    def first_name(self):
        raise AttributeError("Can't delete attribute")

    相关文章

      网友评论

          本文标题:python 属性property

          本文链接:https://www.haomeiwen.com/subject/vdnfpctx.html