intellipaat.com

Object And Class Attributes

Python, like other object-oriented programming languages, has its own way of defining the creation of its classes, in this section we will try to briefly explain how attributes work.

These can be classified by class and instance (object) attributes. Remember that when we refer to class and attribute in an object-oriented programming language, we mean variables encapsulated in classes that attempt to reflect the behavior of a real-life object.

Class Attribute

Class attributes are those that belong to your class and will be shared by all instances of it. Any modification of this attribute will be reflected in all its instances and the way to modify these attributes is by using ClassName.AttributeName.

class A:
msg = "class attr"
a = A()
b = A()
print(a.msg)
print(b.msg)
A.msg = "new class attr val"
print(a.msg)
print(b.msg)
---------------------------------------------------------------
class attr
class attr
new class attr val
new class attr val

Object Attribute

Object attributes are those that belong only to the instance of the class created, for this reason, they are also called instance attributes and its value is not shared by other instances of the same class.

class A:
pass
a = A()
b = A()
a.n = 2
print(a.n)
print(b.n) #exception raised
---------------------------------------------------------------
2
Traceback (most recent call last):
...
AttributeError: 'A' object has no attribute 'n'

In these examples we present a simple way to create public attributes, however it is recommended in almost all cases to use private attributes, either class or object. This you can do in python using __AttributeName. In addition to this use getter and setter functions to access and modify the values of these attributes because in this way rules can be defined within the class to access and modify these variables

class A:
__msg = "class attr"
@property
def msg(self):
return A.__msg
@msg.setter
def msg(self, msg):
A.__msg = msg
a = A()
b = A()
print(a.msg)
print(b.msg)
A.msg = "new val class attr"
print(a.msg)
print(b.msg)
---------------------------------------------------------------
class attr
class attr
new val class attr
new val class attr

This would be the way in which python prefers that the attributes are treated, implementing the tags in this way does not change the way of accessing and modifying the attributes but you earn points in future maintenance of the classes, also this applies to class and object attributes.

To control the attributes of a class, python assigns a mapping object called __dict__, here all instances and their defined attributes will be stored

Differences Between Both Types Of Attributes

As mentioned above, class attributes are shared by instances and their modification affects all instances, while instance attributes are instance specific and may or may not be in other instances. This is the main difference between both types of attributes, however, we can use a method to make an instance attribute present in all instances of a class.

class A:
def __init__(self):
self.__n = 0
@property
def n(self):
return self.__n
@n.setter
def n(self, n):
self.__n = n
a = A()
b = A()
print(a.n)
print(b.n)
a.n = 1
print(a.n)
print(b.n)
---------------------------------------------------------------
0
0
1
0

Using the init method in this way allows the attribute to be an object and found in all instances of the class, however the attribute remains a unique value for each instance, so any changes will only be reflected in the instance in which it was carried out.

All of the aforementioned can be considered an advantage or disadvantage with respect to the work to be done in the program. In some cases the shared use of class attributes can be an advantage over the instance attributes, or in other cases the almost private use of the instance variables can be an advantage over the class attributes.

  • What are the advantages and drawbacks of each of them
  • How does Python deal with the object and class attributes using the __dict__

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store