What is a descriptor in Python?
Mia Morrison
Updated on March 28, 2026
What is a descriptor in Python?
Definition of descriptor : Python descriptors are created to manage the attributes of different classes which use the object as reference. A descriptor is a mechanism behind properties, methods, static methods, class methods, and super() .
What is a attribute in Python?
Attributes of a class are function objects that define corresponding methods of its instances. They are used to implement access controls of the classes. Attributes of a class can also be accessed using the following built-in methods and functions : setattr() – This function is used to set an attribute.
What are __ methods in Python?
__add__ magic method is used to add the attributes of the class instance. For example, let’s say object1 is an instance of a class A and object2 is an instance of class B and both of these classes have an attribute called ‘a’, that holds an integer.
What does __ add __ do in Python?
Syntax. Python’s object. __add__(self, other) method returns a new object that represents the sum of two objects. It implements the addition operator + in Python.
What does Discriptors mean?
Definition of descriptor : something (such as a word or characteristic feature) that serves to describe or identify especially : a word or phrase (such as an index term) used to identify an item (such as a subject or document) in an information retrieval system.
What is a descriptor programming?
In computing, a data descriptor is a structure containing information that describes data. Data descriptors may be used in compilers, as a software structure at run time in languages like Ada or PL/I, or as a hardware structure in some computers such as Burroughs large systems.
How do you create an attribute in Python?
How to add attributes to a class at runtime in Python
- class ObjectClass():
- def __init__(self):
- self. attribute1 = “attribute1”
- def newAttr(self, attr):
- setattr(self, attr, attr)
- objectClass = ObjectClass()
- print(objectClass. attribute1)
- setattr(objectClass, “newAttribute”, “new attr”)
What are attributes in class Python?
A class attribute is a Python variable that belongs to a class rather than a particular object. It is shared between all the objects of this class and it is defined outside the constructor function, __init__(self,…) , of the class.
What is Dunder in Python?
Dunder or magic methods in Python are the methods having two prefix and suffix underscores in the method name. Dunder here means “Double Under (Underscores)”. These are commonly used for operator overloading. These methods are the reason we can add two strings with ‘+’ operator without any explicit typecasting.
Why do we use __ in Python?
The use of double underscore ( __ ) in front of a name (specifically a method name) is not a convention; it has a specific meaning to the interpreter. This is the name mangling that the Python interpreter applies. It does this to protect the variable from getting overridden in subclasses.
What is a Dunder method?
Dunder methods are names that are preceded and succeeded by double underscores, hence the name dunder. They are also called magic methods and can help override functionality for built-in functions for custom classes.
What is __ Radd __?
__radd__ is only called if the left object does not have an __add__ method, or that method does not know how to add the two objects (which it flags by returning NotImplemented ). Both classes have an __add__ method, which do not return NotImplemented .