Thanks to theidioms.com

Learn Python For Data Science (Course I)

Learn Python For Data Science (Course I)

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). A class can be understood as the “blueprint” for creating objects.

A 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. Class instances can also have methods for modifying its state.

Defining a Class in Python

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:
    <statement1> 
    ............ 
    <statementN>

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')

Creating an Object in Python

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

objectName = ClassName()

An object can be instantiated from the DemoClass created earlier. Then, we can access the attribute (x) as well as call the method demoMethod as shown below.

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

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

# 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) an object’s initial attributes 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)

my_class_object1.value_show()
my_class_object2.value_show()

# Printing the address of the object
print(my_class_object1)
OUTPUT:
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 defined 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 to have a unique name for each and every object in Python. The example below 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"
    do_local()
    print("After local assignment:", spam)
    do_nonlocal()
    print("After nonlocal assignment:", spam)
    do_global()
    print("After global assignment:", spam)

scope_test()
print("In global scope:", spam)

OUTPUT:
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, i.e., the concept of Classes and Objects. In the next chapter, you will be getting an overview of some of the standard libraries used in Python and their uses.

Leave your thought here

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

Close Bitnami banner
Bitnami