How object and class attributes work in Python

Introduction

In this article, we are going to discuss how object and class attributes work in Python. We will learn about Python objects and classes. We will learn what a class is, what an object is and how to create and use them in our programs.

What is an Object?

Python is an object oriented programming language. Unlike procedure oriented programming, where the main emphasis is on functions, object oriented programming stress on objects. Object is simply a collection of data (variables) and methods (functions) that act on those data. And, class is a blueprint for the object. We can think of class as a sketch (prototype) of a house. It contains all the details about the floors, doors, windows etc. Based on these descriptions we build the house. House is the object. As, many houses can be made from a description, we can create many objects from a class. An object is also called an instance of a class and the process of creating this object is called instantiation.

What is a Class?

A Class in Python is like an object constructor, or a "blueprint" for creating objects. A Python class is for defining a particular type of object. Because Python objects can have both function and data elements, Python classes define what methods can be used to change the state of an object. They also indicate what attributes the object can have.

According to Python Documentations, Classes provide a means of bundling data and functionality together. Creating a new class creates a new type of object, allowing new instances of that type to be made. Each class instance can have attributes attached to it for maintaining its state.

How to create a Class in Python

Like function definitions begin with the keyword def, in Python, we define a class using the keyword class. The first string is called docstring and has a brief description about the class. Although not mandatory, this is recommended. Here is a simple class definition.
class MyNewClass:
'''This is a docstring. I have created a new class'''
pass

How to create an Object in Python

We saw that the class object could be used to access different attributes. It can also be used to create new object instances (instantiation) of that class. The procedure to create an object is similar to a function call.
ob = MyNewClass()

This will create a new instance object named ob. We can access attributes of objects using the object name prefix. Attributes may be data or method. Method of an object are corresponding functions of that class. Any function object that is a class attribute defines a method for objects of that class.

Example program to demonstrate class and object is as below:
class MyNewClass:
"This is my second class"
a = 10
def func(self):
print('Hello World!')
# create a new MyClass
ob = MyNewClass()
# Calling function func()
# Output: Hello
ob.func()

Running this would output without the quotes, "Hello World!"

Difference between class and instance attributes

Class attributes are variables of a class that are shared between all of its instances. They differ from instance attributes in that instance attributes are owned by one specific instance of the class only, and are not shared between instances.

Instance Attributes are unique to each object, (an instance is another name for an object). Here, any Dog object we create will be able to store its name and age. We can change either attribute of either dog, without affecting any other dog objects we've created.

How does Python deal with the object and class attributes using the __dict__

Every object in Python has an attribute denoted by __dict__. This dictionary/dictionary-like (I will explain this shortly) object contains all the attributes defined for the object itself. It maps the attribute name to its value. Heres an example:
class MyClass(object):
class_var = 1
def __init__(self, i_var):
self.i_var = i_var
foo = MyClass(2)
bar = MyClass(3)
print MyClass.__dict__
print foo.__dict__
print bar.__dict__

This gives the output:
{'__module__': '__main__', 'class_var': 1, '__dict__': , '__weakref__': , '__doc__': None, '__init__': }
{'i_var': 2}
{'i_var': 3}

Thats all for now folks. Happy Coding!

Advertisement
Advertisement
Advertisement
More Articles