类属性:类属性对于每个类都是唯一的。该类的每个实例都将具有此属性。
例子:
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 装饰器,您可以将其添加到方法上方。 |
相关用法
- Python attr.asdict()用法及代码示例
- Python abs()用法及代码示例
- Python any()用法及代码示例
- Python all()用法及代码示例
- Python ascii()用法及代码示例
- Python append() and extend()用法及代码示例
- Python argparse.ArgumentParser.add_subparsers用法及代码示例
- Python argparse.ArgumentParser.add_argument_group用法及代码示例
- Python argparse.ArgumentParser.add_mutually_exclusive_group用法及代码示例
- Python argparse.ArgumentParser.set_defaults用法及代码示例
- Python argparse.ArgumentParser.get_default用法及代码示例
- Python argparse.ArgumentParser.convert_arg_line_to_args用法及代码示例
- Python argparse.ArgumentParser.exit用法及代码示例
- Python argparse.FileType用法及代码示例
- Python ast.AST用法及代码示例
- Python ast.Constant用法及代码示例
- Python ast.JoinedStr用法及代码示例
- Python ast.List用法及代码示例
- Python ast.Set用法及代码示例
- Python ast.Dict用法及代码示例
- Python ast.Load用法及代码示例
- Python ast.Starred用法及代码示例
- Python ast.Expr用法及代码示例
- Python ast.BinOp用法及代码示例
- Python ast.BoolOp用法及代码示例
注:本文由纯净天空筛选整理自mayanktyagi1709大神的英文原创作品 Difference between attributes and properties in Python。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。