Thanks to

Classes and Objects in Python

Learn Python for Data Science

Classes and Objects in Python

In this chapter, you will learn about classes and objects in Python.

Python is an object-oriented programming language. Python focuses on entities known as objects unlike functions in procedure-oriented programming languages.

Classes and Objects

An object is a collection of data (variables) and methods (functions). Consider a class as the “blueprint” for creating objects.

A class creates a new type of object, allowing you to create new instances of that object type. Each class instance can have attributes and methods to maintain and modify its state, respectively.

In Python, a class can be defined by using the keyword class. The simplest form of class definition looks like this:

 # Creating a class in Python
 class ClassName:

The following example illustrates a class that has an attribute (variable) x and a method (function) demoMethod:

class DemoClass:
    x = 0 # Attribute
    def demoMethod(self): # Method
        print('Method successfully called')

Objects can be instantiated from an already defined class. The general syntax for creating an object from a class is as follows.

objectName = ClassName()

The following example demonstrates how you can instantiate an object from the DemoClass created earlier. Then, you can access the attribute (x) as well as call the method demoMethod.

# Instantiating an object
>>> obj1 = DemoClass()

# Accessing the attribute
>>> print(obj1.x) 

# Calling the method (function)
>>> obj1.demoMethod() 
'Method successfully called'

The __init__() Method

All classes create objects, and all objects contain characteristics called attributes. We use the __init__() method to initialize (specify) the initial attributes of an object by giving them their default value (or state). These values are automatically initialized when an object is instantiated from a class.

The following example illustrates the use of the __init__() method.

#Simple class implementation in Python

class MyClass(object):
    def __init__(self, val):
       # Value initialization
        self.value = val
    def value_show(self):
        print("The value of my class is",self.value)

my_class_object1 = MyClass(365)
my_class_object2 = MyClass(48)


# Printing the address of the object
The value of my class is 365
The value of my class is 48
<__main__.Myclass object at 0x000001F0C57050F0>

From the above example, we can also see that the value of attributes differs for each object in a class. In other words, each object has its own values of the attributes specified in the class definition.

Scopes and Namespaces

Scope refers to the coding region from which a particular Python object is accessible. A namespace is a system that ensures that each and every Python object has a unique name. The following example illustrates this:

# An example of Scopes and Namespaces 

def scope_test():
    def do_local():
        spam = "local spam"

    def do_nonlocal():
        nonlocal spam
        spam = "nonlocal spam"

    def do_global():
        global spam
        spam = "global spam"

    spam = "test spam"
    print("After local assignment:", spam)
    print("After nonlocal assignment:", spam)
    print("After global assignment:", spam)

print("In global scope:", spam)

After local assignment: test spam
After nonlocal assignment: nonlocal spam
After global assignment: nonlocal spam
In global scope: global spam

You are now familiar with one of the most important topics in Python programming, that is, the concept of Classes and Objects in Python. In the next chapter, we will introduce the ‘Standard libraries used in Python‘, along with their uses.

Leave your thought here

Your email address will not be published. Required fields are marked *