Python property()函数

Python property()函数 返回 调查对象 所有物 它用于创建类的属性。

null

语法: 财产(fget、fset、fdel、doc)

参数:

  • fget() –用于获取属性的值
  • fset() –用于设置属性的值
  • fdel() –用于删除属性值
  • 博士() –包含属性文档(docstring)的字符串

返回: 从给定的 getter、setter和deleter。

注:

  • 如果没有给出任何论据, 财产() 方法返回不包含任何getter、setter或deleter的基属性。
  • 如果没有提供doc,property()方法将获取getter函数的docstring。

示例#1: 使用property()方法

Python3

# Python program to explain property() function
# Alphabet class
class Alphabet:
def __init__( self , value):
self ._value = value
# getting the values
def getValue( self ):
print ( 'Getting value' )
return self ._value
# setting the values
def setValue( self , value):
print ( 'Setting value to ' + value)
self ._value = value
# deleting the values
def delValue( self ):
print ( 'Deleting value' )
del self ._value
value = property (getValue, setValue,
delValue, )
# passing the value
x = Alphabet( 'GeeksforGeeks' )
print (x.value)
x.value = 'GfG'
del x.value


输出:

Getting valueGeeksforGeeksSetting value to GfGDeleting value

Python属性 使用 室内装修设计师

装饰器的主要工作是将功能添加到现有代码中。也称为元编程,因为程序的一部分试图在编译时修改程序的另一部分。

示例2: 使用@property decorator

Python3

# Python program to explain property()
# function using decorator
class Alphabet:
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
# passing the value
x = Alphabet( 'Peter' )
print (x.value)
x.value = 'Diesel'
del x.value


输出:

Getting valuePeterSetting value to DieselDeleting value

使用@property decorator的工作原理与property()方法相同。

首先,指定value()方法也是Alphabet的一个属性,然后,我们使用属性值来指定 Python属性设置器 还有删除者。请注意,相同的方法value()用于定义getter、setter和deleter的不同定义。无论何时使用x.value,它都会在内部调用相应的getter、setter和deleter。

Python属性vs属性

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

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)


输出:

122

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

Python属性(): 退换商品 调查对象 所有物

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 valueHappy Coding!Setting value to Hey Coder!Deleting value

应用

通过使用property()方法,我们可以修改类并实现值约束,而无需对客户机代码进行任何更改。因此,实现是向后兼容的。

© 版权声明
THE END
喜欢就支持一下吧
点赞12 分享