Home >>Python Built-in Functions >Python property() function

Python property() function

Python property() function

Python property() function is used to create property of a given class. If no arguments are given then it returns a base property attribute that doesn’t contain any getter, setter or deleter.

Syntax:
property(fget, fset, fdel, doc)

Parameter Values

Parameter Description
fget This parameter is used to get the value of attribute
fset This parameter is used to set the value of attribute
fdel This parameter is used to delete the attribute value
doc This parameter is a string that contains the documentation (docstring) for the attribute
Here is an example of Python property() function:

class Alphabet: 
	def __init__(self, value): 
		self._value = value 
		
	def getValue(self): 
		print('Getting value') 
		return self._value 

	def setValue(self, value): 
		print('Setting value to ' + value) 
		self._value = value 
		
	def delValue(self): 
		print('Deleting value') 
		del self._value 
	
	value = property(getValue, setValue, delValue, ) 

x = Alphabet('PHPTPOINT') 
print(x.value) 

x.value = 'Abhi'

del x.value

Output:
Getting value
PHPTPOINT
Setting value to Abhi
Deleting value

Python Built-in Functions Python abs() Function Python all() Function Python any() Function Python ascii() Function Python bin() Function Python bool() Function Python bytearray() Function Python bytes() Function Python callable() Function Python chr() Function Python compile() Function Python complex() Function Python delattr() Function Python dict() Function Python dir() Function Python divmod() Function Python enumerate() Function Python eval() Function Python exec() Function Python filter() Function Python float() Function Python format() Function Python frozenset() Function Python getattr() Function Python globals() Function Python hasattr() Function Python hash() Function Python help() Function Python hex() Function Python id() Function Python input() Function Python int() Function Python isinstance() Function Python issubclass() Function Python iter() Function Python len() Function Python list() Function Python locals() Function Python map() Function Python max() Function Python memoryview() Function Python min() Function Python next() Function Python object() Function Python oct() Function Python open() Function Python ord() Function Python pow() Function Python print() Function Python property() function Python range() Function Python repr() Function Python reversed() Function Python round() Function Python set() Function Python setattr() Function Python slice() Function Python sorted() Function Python str() Function Python sum() Function Python super() Function Python tuple() Function Python type() Function Python vars() Function Python zip() Function
No Sidebar ads