python中property函数的功能

550次阅读
没有评论

python中property函数的功能

1、property() 函数

如果为 python类定义了getter、setter等访问器方法,则可使用 property() 函数将它们定义成属性(相当于实例变量);

即property() 函数的功能就是定义属性。

2、函数原型

class property(fget=None, fset=None, fdel=None, doc=None)

3、语法

class property ( [fget [, fset [, fdel [, doc] ] ] ] )

4、参数

fget:获取属性值的函数

fset:设置属性值的函数

fdel:删除属性值函数

doc:属性描述信息

5、使用实例

class Property():   # 这个函数也不是固定的可以更改
    def fget(self):
        return self._name

    def fset(self, name):
        self._name = name.upper()

    def fdel(self):   # 函数定义的参数不是固定的可以更改 
        self._name = "NoName"

    name = property(fget, fset, fdel, "对name进行下操作")

pl = Property()
pl.name = "abcd"  # 这个整体的函数作用是让这个里边的字母变成大写
print(pl.name)
神龙|纯净稳定代理IP免费测试>>>>>>>>天启|企业级代理IP免费测试>>>>>>>>IPIPGO|全球住宅代理IP免费测试

相关文章:

版权声明:wuyou2021-05-11发表,共计613字。
新手QQ群:570568346,欢迎进群讨论 Python51学习