Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C Code for Python's property function?

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.

like image 510
Marcos Gonzalez Avatar asked May 08 '13 18:05

Marcos Gonzalez


People also ask

What is property () in Python?

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.

How do you print a property in Python?

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.

How do you access properties in Python?

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.

How do you return a property in Python?

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.


1 Answers

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.

like image 104
Martijn Pieters Avatar answered Oct 11 '22 12:10

Martijn Pieters