Reputation: 113
I am currently writing a paper on python fundamentals and I need some help on it as I could not find a single consistent definition for Attribute
class Person:
def __init__(self,weight):
self.weight = weight
An attribute is something that describes an object for example weight = 75. In this case, what is an attribute exactly is it 75 or is it weight(the variable) or is the combination of both the variable and the value.
Some people say attribute is the data stored in the object, which is correct if we consider 75 to be an attribute.
Some people call attribute to be instance and class variables that store data pertaining to the class and the instance, which is correct if we consider weight to be an attribute(the variable name)
Some call attribute the combination of name and a value which means weight = 75 as a whole becomes the attribute
All the above definitions are slightly different if we go by their literal meaning and people use them interchangeably
Am I missing something??
I just need to be sure before I send the draft to my HOD
Thank you, everyone
Please do take good care of yourself and avoid traveling as much as possible Chyanit
Upvotes: 2
Views: 5151
Reputation: 333
First let's talk about general objects in Python. The documentation defines 3 characteristics for every object:
An object’s identity never changes once it has been created; you may think of it as the object’s address in memory.
An object’s type determines the operations that the object supports (e.g., “does it have a length?”) and also defines the possible values for objects of that type. The type() function returns an object’s type (which is an object itself). Like its identity, an object’s type is also unchangeable.
The value of some objects can change. Objects whose value can change are said to be mutable; objects whose value is unchangeable once they are created are called immutable.
Understanding objects means understanding types, but types are objects also. These TypeObjects are one of the most important structures in Python and define the basic functionality of an object. Specifically they implement the getattr and setattr functions.
The default behavior for this implementation is described here:
The default behavior for attribute access is to get, set, or delete the attribute from an object’s dictionary. For instance,
a.x
has a lookup chain starting witha.__dict__['x']
, thentype(a).__dict__['x']
, and continuing through the base classes oftype(a)
excluding metaclasses. If the looked-up value is an object defining one of the descriptor methods, then Python may override the default behavior and invoke the descriptor method instead. [...]
the methods for an class or an instance which can overwrite the default behaviour are __get__
, __set__
and __delete__
.
The documentation for the __get__
method specifies:
This method should return the computed attribute value or raise an AttributeError exception.
Summarizing all the above, my formal definition for an attribute would be along the lines of:
Further reading:
https://docs.python.org/3/reference/datamodel.html
https://docs.python.org/3/c-api/typeobj.html
https://docs.python.org/3/howto/descriptor.html?highlight=descriptor
https://realpython.com/python-descriptors/
Upvotes: 1
Reputation: 50076
TLDR: The attribute is the value produced by looking up the attribute name.
According to the Python Language Reference, Attribute references, in the statement
value = obj.name
obj.name
is an attribute reference, name
is the attribute name, and the produced value
is the attribute. Note that value
is the attribute in this case because it was produced by the attribute reference, not because it is inherently related. For example, an attribute reference may produce a new value
every time it is evaluated.
The primary must evaluate to an object of a type that supports attribute references, which most objects do. This object is then asked to produce the attribute whose name is the identifier. This production can be customized by overriding the
__getattr__()
method. If this attribute is not available, the exception AttributeError is raised. Otherwise, the type and value of the object produced is determined by the object. Multiple evaluations of the same attribute reference may yield different objects.
In an attribute assignment such as
obj.name = value
the value
does not necessarily become the attribute. If the attribute name points to a data descriptor, value
may be stored, modified or discarded.
Upvotes: 3
Reputation: 1
Basically, an attribute is characteristic of an entity/object. It describes the qualities of an object.
For example, if you have a Employee class having employee_id as X1234, this employee_id is a characteristic of the employee, which can be used to identify an employee.
Upvotes: 0