I am really curious as to how Python's interpreter makes an attribute x
out of a method x
through x=property(x)
. If I could take a look at the C
code, I would feel much better.
Python property() function returns the object of the property class and it is used to create property of a class. Syntax: property(fget, fset, fdel, doc) Parameters: fget() – used to get the value of attribute. fset() – used to set the value of attribute.
Use Python's dir to Print an Object's Attributes One of the easiest ways to access a Python object's attributes is the dir() function. This function is built-in directly into Python, so there's no need to import any libraries.
Attributes of a class can also be accessed using the following built-in methods and functions : getattr() – This function is used to access the attribute of object. hasattr() – This function is used to check if an attribute exist or not. setattr() – This function is used to set an attribute.
Return value from property()property() returns the property attribute from the given getter, setter, and deleter. If no arguments are given, property() returns a base property attribute that doesn't contain any getter, setter or deleter. If doc isn't provided, property() takes the docstring of the getter function.
The type is defined in the descrobject.c
file.
You can locate Python types like these by first looking for the function name in bltinmodule.c
; in this case the following line defines the property()
function:
SETBUILTIN("property", &PyProperty_Type);
then grep for the PyProperty_Type
definition in the Objects
subdirectory.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With