python属性_Python属性()

python属性

Python property() function returns a property attribute. It’s mostly used to create a manageable class attribute.

Python property()函数返回一个property属性。 它主要用于创建可管理的属性。

Python属性() (Python property())

Python property() function syntax is:

Python property()函数语法为:

class property(fget=None, fset=None, fdel=None, doc=None)
  1. fget: function for getting the attribute value

    fget :获取属性值的函数
  2. fset: function to set the attribute value

    fset :用于设置属性值的函数
  3. fdel: function to delete the attribute

    fdel :删除属性的功能
  4. doc: creates the docstring for the attribute to be used in help() function.

    doc :为将在help()函数中使用的属性创建docstring。

Let’s look at an example to create property attribute in a class.

让我们看一个在类中创建属性属性的示例。

class Person:

    def __init__(self):
        self._name = None

    def get_name(self):
        print('get_name called')
        return self._name

    def set_name(self, i):
        print('set_name called')
        self._name = i

    def del_name(self):
        print('del_name called')
        del self._name

    name = property(get_name, set_name, del_name, "Person's Name Attribute")


d = Person()

d.name = 'Pankaj'

print(d.name)

del d.name

Output:

输出:

set_name called
get_name called
Pankaj
del_name called

Notice that Person name property is managed through specified methods and it’s using the _name private property of Person.

注意,Person名称属性是通过指定的方法进行管理的,并且使用了Person的_name私有属性。

We can create a read-only attribute or non-deletable attribute using property function.

我们可以使用属性函数创建只读属性或不可删除属性。

If we define property function as:

如果我们将属性函数定义为:

name = property(get_name, set_name, None)

Then del d.name will throw exception as AttributeError: can't delete attribute.

然后del d.name将抛出异常,因为AttributeError: can't delete attribute

Similarly, if we define property attribute as:

同样,如果我们将property属性定义为:

name = property(get_name, None, None)

The name will be read-only. If we try to set its value using d.name = 'Pankaj' then exception will be thrown as AttributeError: can't set attribute.

该名称将是只读的。 如果我们尝试使用d.name = 'Pankaj'设置其值, d.name = 'Pankaj'异常AttributeError: can't set attribute

Python属性装饰器 (Python Property Decorator)

We can use @property decorator to achieve the same thing.

我们可以使用@property装饰器来实现相同的目的。

class Data:
    def __init__(self):
        self._id = None

    @property
    def id(self):
        print('id getter')
        return self._id

    @id.setter
    def id(self, i):
        print('id setter')
        self._id = i

    @id.deleter
    def id(self):
        print('id deleter')
        del self._id


d = Data()

d.id = 100
print(d.id)

del d.id

Output:

输出:

id setter
id getter
100
id deleter
GitHub Repository. GitHub存储库中检出完整的python脚本和更多Python示例。

Reference: Official Documentation

参考: 官方文档

翻译自: https://www.journaldev.com/23011/python-property

python属性

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值