Python. How object and class attributes work?

Campo Elías Pinillos Galindo
4 min readJan 17, 2020

--

Object-oriented programming is one of the most effective approaches to writing code. In in this approach we set classes and then we create objects based on these classes. When you write a class, you define a general description and the functionality of a whole category of what objects can have. When you create individual objects from the class, each object is automatically equipped with this description and functionality. Making an object from a class is called instantiation, and you work with instances of a class.

Python, as an object-oriented programming language, provides two scopes for attributes: class attributes and instance attributes.

  • What’s a class attribute?A class attribute is a Python variable that belongs to a class rather than an object. It is shared between all the objects of this class and it is defined outside the constructor function, _init__(self,...), of the class.” DZone. Web Dev Zone.
  • What’s an instance attribute? “An instance attribute is a Python variable belonging to one, and only one, object. This variable is only accessible in the scope of this object and it is defined inside the constructor function, __init__(self,..) of the class.” DZone. Web Dev Zone.
  • What are all the ways to create them and what is the Pythonic way of doing it? Let’s see the code in action:

In this case we define a class called Dog. Then is a docstring describing what this class does. After that, dog_number the class attributesis shared between all the objects of the class. Then we define the __init__() method to have three parameters: self, name, and age. The self parameter is required in the method definition, and it must come first before the other parameters. Every method call associated with a class automatically passes self, which is a reference to the instance itself, it gives the individual instance access to the attributes and methods in the class. Finally, the two variables defined have the prefix self. Any variable prefixed with self is available to every method in the class, and we’ll also be able to access these variables through any instance created from the class. self.name = name takes the value stored in the parameter name and stores it in the variable name. The same process happens with self.age = age. Variables that are accessible through instances like this are called instance attributes.

  • What are the differences between class and instance attributes? So, the main difference is while instance attributes are only accessible from the scope of the object, class attributes are accessible as both a property of the class and as a property of objects, as it is shared between all of them.
  • What are the advantages and drawbacks of each of them? One of the main advantage is the possibility to use and reuse this attributes whenever the programmer wants to do, writing less code. The other advantage is the inheritance ability, so you can create a parent class with certain attributes like mammals and create child classes which not only inherits the parent attributes but also they can have their own attributes like hummans or another animal in the class mammals. It is important to know that the class attribute can be accessed as a class property and as an instance property, however, accessing an instance attribute as a class property must raise an AttributeError.
  • How does Python deal with the object and class attributes using the __dict__? If we refer againg to the Pythonic way of doing things and the Zen of Python the last line states: “Namespaces are one honking great idea — let’s do more of those!”. A namespace is a mapping between objects and names. To keep it simple, let’s say it is a Python dictionary that has as a key to the name of the object and its value as a value. Different namespaces can coexist with the property while the names within them are independent. Python classes and objects have different namespaces, for our example, we have Dog.__dict__ as a namespace for our class and Somenamedog.__dict__as a namespace for our object Somenamedog. “When you access an attribute (instance or class attribute) as a property of an object using the dot convention, it searches first in the namespace of that object for that attribute name. If it is found, it returns the value, otherwise, it searches in the namespace of the class. If nothing is found there as well, it raises an AttributeError. The object namespace is before the class namespace. If we find, in one class, both an instance attribute and a class attribute with the same name, the access to that name from your object will get you the value in the object namespace. Below a simplified version of the lookup function. When you access an attribute as a class property, it searches directly in the class namespace for the name of that attribute. If it is found, it returns the value, otherwise, it raises an AttributeError”.

OOP!!!!

Cheers!

Source:

https://dzone.com/articles/python-class-attributes-vs-instance-attributes

--

--