Learn Python for Data Science (Course I)July 18, 2020 2020-08-26 9:26
Learn Python for Data Science (Course I)
- Welcome to Course I!
- Installing Python and Getting Started
- Data Types and Operations in Python
- Control Flow Tools in Python
- Errors and Exceptions in Python
- Classes and Objects in Python
- Standard Libraries in Python
- Virtual Environments and Packages in Python
- Working with Files in Python
- End of Course
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: <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')
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) 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) 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) 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 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" 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 programming, that is, the concept of Classes and Objects in Python. In the next chapter, we will introduce standard libraries used in Python and their uses.