Python is an object-oriented programming language. We can easily create and use classes and objects in Python
Class
Defining Classes
The following statement makes a class with no attributes attached, and in fact, it's an empty namespace object:
The name of this class is Student, and it doesn't inherit from any other class. Class names are usually capitalized, but this is only a convention, not a requirement. Everything in a class is indented, just like the code within a function, if statement, for loop, or any other block of code. The first line not indented is outside the class.
class Student:
pass
>>> Student.name = "Umesh" >>> Student.id = 23415
In Python, objects are created in two steps:
Constructs an object
__new()__
Initializes the object
__init()__
However, it's very rare to actually need to implement __new()__ because Python constructs our objects for us. So, in most of the cases, we usually only implement the special method, __init()__.
Let's create a class that stores a string and a number:
class Student(object): '''Classes can (and should) have docstrings too, just like modules and functions''' def __init__(self, name, id = 20001): self.name = name self.id = id
When a def appears inside a class, it is usually known as a method. It automatically receives a special first argument, self, that provides a handle back to the instance to be processed. Methods with two underscores at the start and end of names are special methods.
The __init__() method is called immediately after an instance of the class is created. It would be tempting to call this the constructor of the class. It's really tempting, because it looks like a C++ constructor, and by convention, the __init__() method is the first method defined for the class. It appears to be acting like a constructor because it's the first piece of code executed in a newly created instance of the class. However, it's not like a constructor, because the object has already been constructed by the time the __init()__ method is called, and we already have a valid reference to the new instance of the class.
The first parameter of __init()__ method, self, is equivalent to this of C++. Though we do not have to pass it since Python will do it for us, we must put self as the first parameter of nonstatic methods. But the self is always explicit in Python to make attribute access more obvious.
The self is always a reference to the current instance of the class. Though this argument fills the role of the reserved word this in c++ or Java, but self is not a reserved word in Python, merely a naming convention. Nonetheless, please don't call it anything but self; this is a very strong convention.
When a method assigns to a self attribute, it creates an attribute in an instance because self refers to the instance being processed.
Here is an example of a class Rectangle with a member function returning its area.
class Rectangle(object): def __init__(self, w, h): self.width = w self.height = h def area(self): return self.width * self.height >>> rect = Rectangle(100,20) >>> rect.area() 2000 >>> rect.height = 30 >>> rect.area() 3000
Note that this version is using direct attribute access for the width and height.