2

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

2
  • 1
    Take note that while the meaning of attribute happens to be well-defined in Python, colloquial descriptions do use practically all meanings. In specific, do not expect that your HOD follows the correct definition. Commented Mar 13, 2020 at 22:00
  • I’m guessing you read docs.python.org/3/tutorial/classes.html ? Commented Mar 14, 2020 at 1:58

3 Answers 3

4

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.

Sign up to request clarification or add additional context in comments.

7 Comments

4 hours of searching the docs and i couldn't find the Attribute Reference you posted. Thanks for the link! One confusion i still have is. > Otherwise, the type and value of the object produced is determined by the object. Meaning the "produced value" has a type. So formally speaking a value is an object too?
Yes, python only has objects, and every object has a type.
@MisterMiyagi thank you so much for the explanation really appreciate it.
@IjonTichy Yes, we do refer to value as the object itself. However, in most cases you will use the term value as being the property of an object. For eg an integer object with value 5. The object that is created is empty first and when we tac values to the object, later on, We do so by using the attributes
@ChyanitSingh That's outright false. An integer object always represents a value, it can never be empty and one cannot change the value. In Python, the value 5 is an object.
|
1

First let's talk about general objects in Python. The documentation defines 3 characteristics for every object:

  • identity

    An object’s identity never changes once it has been created; you may think of it as the object’s address in memory.

  • type

    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.

  • value

    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 with a.__dict__['x'], then type(a).__dict__['x'], and continuing through the base classes of type(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:

  • attributes are objects with identity, type and value
  • they are owned by another object

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/

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.