当前位置: 首页>>编程示例 >>用法及示例精选 >>正文


Python attributes和properties的区别用法及代码示例

类属性:类属性对于每个类都是唯一的。该类的每个实例都将具有此属性。

例子:

Python3


# declare a class 
class Employee:  
    
    # class attribute 
    count = 0      
        
    # define a method 
    def increase(self):  
        Employee.count += 1
    
# create an Employee  
# class object 
a1 = Employee()  
  
# calling object's method 
a1.increase()  
  
# print value of class attribute 
print(a1.count)  
     
a2 = Employee()  
  
a2.increase()  
  
print(a2.count)  
    
print(Employee.count)

输出:

1
2
2

在上面的例子中,count变量是一个类属性。

实例属性:实例属性对于每个实例都是唯一的(实例是对象的另一个名称)。每个对象/实例都有自己的属性,并且可以在不影响其他实例的情况下进行更改。

例子:

Python3


# create a class 
class Employee:  
    
    # constructor 
    def __init__(self):  
        
        # instance attribute 
        self.name = 'Gfg'
        self.salary = 4000
    
    # define a method 
    def show(self):  
        print(self.name)  
        print(self.salary)  
  
# create an object of  
# Employee class 
x = Employee() 
  
# method calling 
x.show()

输出:

Gfg
4000

现在,让我们看一个有关属性的示例:

1)使用property()函数创建类的属性:

用法: property(fget, fset, fdel, doc)

例子:

Python3


# create a class 
class gfg:  
    
    # constructor 
    def __init__(self, value):  
        self._value = value  
            
    # getting the values  
    def getter(self):  
        print('Getting value')  
        return self._value  
            
    # setting the values  
    def setter(self, value):  
        print('Setting value to ' + value)  
        self._value = value  
            
    # deleting the values  
    def deleter(self):  
        print('Deleting value')  
        del self._value  
      
    # create a properties 
    value = property(getter, setter, deleter, )  
    
# create a gfg class object 
x = gfg('Happy Coding!')  
print(x.value)  
    
x.value = 'Hey Coder!'
    
# deleting the value 
del x.value

输出:

Getting value
Happy Coding!
Setting value to Hey Coder!
Deleting value

2)使用@property装饰器创建类的属性:

我们可以使用@property装饰器来应用属性函数。这是内置装饰器之一。装饰器只是一个函数,它接受另一个函数作为参数,并通过包装它来添加它的行为。

例子:

Python3


# create a class 
class byDeco:  
    
   # constructor 
    def __init__(self, value):  
        self._value = value  
            
    # getting the values 
    @property              
    def value(self):  
        print('Getting value')  
        return self._value  
            
    # setting the values      
    @value.setter  
    def value(self, value):  
        print('Setting value to ' + value)  
        self._value = value  
            
    # deleting the values  
    @value.deleter  
    def value(self):  
        print('Deleting value')  
        del self._value  
    
    
# create an object of class 
x = byDeco('happy Coding')  
print(x.value)  
    
x.value = 'Hey Coder!'
    
# deleting the value 
del x.value

输出:

Getting value
happy Coding
Setting value to Hey Coder!
Deleting value

属性与属性之间的差异表

Attribute

Property

属性由数据变量说明,例如姓名、年龄、身高等。 属性是特殊类型的属性。

两种类型的属性:

  • 类属性
  • 实例属性
它有 getter、setter 和 delete 方法,如 __get__、__set__ 和 __delete__ 方法。
Class attributes 通常在类主体部分中定义在顶部。 我们可以使用以下方法定义 getter、setter 和 delete 方法property() 函数。
I实例属性在类主体中定义使用self关键字通常是__init__()方法。 如果我们只想读取属性,还有一个 @property 装饰器,您可以将其添加到方法上方。


相关用法


注:本文由纯净天空筛选整理自mayanktyagi1709大神的英文原创作品 Difference between attributes and properties in Python。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。