Python Classes and Methods
Python is an object-oriented programming language.
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. Class instances also have methods (defined by its class) for modifying its state.
https://docs.python.org/3/tutorial/classes.html
Imagine the python class as a “blueprint of a house” or an object constructor and it consists of all the information (object) i.e, properties and methods. As a result, the class is to define the blueprint, so that every object of the same type has the same characteristics.
Contents
How to create a Class?
An object is created using the constructor of the class. This object will then be called the instance
of the class. To create a class, use the keyword class.
Instance = class(arguments)
Create a class named number, with a property named x:
Empty class with no functionalities,
class Number:
pass
number = Number()
print(number)
Attributes and Method
If there is no functionality set to the class, there is no use of class at all. The functionalities are defined through the attributes. The attributes are the data and function container. The attributes are inside the class.
Attributes
class Number:
integer = "zero"
numericalvalue = Number()
print(numericalvalue.integer)
- From the above code, “integer” is the name of the attribute.
- Object instantiation: Assigning the class to the variable “numericalvalue”
- Accessing the attributes using the dot operator.
Method
Now you will define a function that will access the class attributes. These functions which access the attributes are Methods. When these methods created, you will always start the first argument to the method as the self keyword.
class Student:
# creating a variable
name = "IoTEDU"
# creating a function
def website(self, blog):
self.name = blog
student= Student()
print (student.name)
student.website("iot4beginners")
print (student.name)
classmethod()
The classmethod()
is an inbuilt function in Python which returns a class method for given function.
class Student:
# creating a variable
name = "IoTEDU"
# creating a function
def website(blog):
print("The name of the website is : ", blog.name)
Student.website = classmethod (Student.website)
Student.website()
__init__() Function
__init__() is the constructor, basically a function to “initialize”/“activate” the properties of the class for a specific object, once created and matched to the corresponding class.
The __init__() function is called everytime when a class creates a new object.
class Complex:
def __init__(self, realpart, imagpart):
self.r = realpart
self.i = imagpart
x = Complex(3.0, -4.5)
print (x.r, x.i)
Self Parameter
In our previous example code, we saw the word “self”. What does that word mean? The “self” parameter is a reference to current instances of a class. When it is to access the variables of the class.
It does not need to be really “self”, you can call it whatever you like. It is just a placeholder. You use “self” to improve the readability of the program.
The difference between both the constructors are,
- __init__ is basically a function that will “initialize”/”activate” the properties of the class for a specific object, once created and matched to the corresponding class.
self
represents that object which will inherit those properties.
Pass Statements
If you have a class, and then there are no contents, it is likely to be raising an error. So, you can use the keyword “pass” to avoid such errors.
class Number:
pass