Python中属性和属性之间的区别
类属性:类属性对于每个类都是唯一的。类的每个实例都将具有此属性。
例子:
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)
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()
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
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
输出:
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()函数创建类的属性:
Syntax: 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 V/s Property之间的区别表
Attribute | Property |
---|---|
Attributes are described by data variables for example like name, age, height etc. | Properties are special kind of attributes. |
Two types of attributes:
| It has getter, setter and delete methods like __get__, __set__ and __delete__ methods. |
Class attributes are defined in the class body parts usually at the top. | We can define getters, setters, and delete methods with the property() function. |
Instance attribute are defined in the class body using self keyword usually it the __init__() method. | If we just want to the read property, there is also a @property decorator which you can add above your method. |