In Python, by creating a class we could define a new type of objects.
A class is defined with a set of attributes, such as methods and instance variables, which are shared by each object of the class.
A method is a place where the logic of a class is stored and a class may have zero or more methods defined in it.
An instance variable is used to represent the state of each object of the class. A class may also have one or more instance variables declared in it.
Before we create a class, let us understand the naming conventions set by Python for a class and its attributes.
Python naming conventions -
Technically, every class name in Python should start with a capital alphabet and the first alphabet of a
multi-word name should be in the capital too.
Every method defined in a Python class should be in lowercase and multi-word names are separated by an underscore.
Every instance variable defined in a Python class should be in lowercase and multi-word names are separated by an underscore.
Note :
Python compiler doesn't throw a compile error if a class name starts with a lowercase letter, but it's still better to follow the naming conventions defined in Python.
Python is a case-sensitive language, hence a class containing two instance variables with one named a and the other named A, are treated differently.
A class with a method
We are going to create a class within a Python program which is saved in a file named First.py.
We are also going to define a method that belongs to the class.
Why .py extension for Python program?
It is because py is taken from the word python.
First.py
# Python - Creating a class with methods and instance variables
class Test:
# Defining a method named message of class Test
def message(self):
print('Hello from Test')
ob = Test()
ob.message()
Program Analysis
Let us break the code and understand what exactly we have done in the last program :
class Test:
We have created a class named Test. In Python, creating a class begins with statement class, followed by a space,
followed by the name of a class, followed by a colon :
Note:Next, the elements/properties of a class i.e. method and instance variables, should begin with an indentation or a syntax error is reported.
# Defining a method named msg of class Test
def message(self):
print('Hello from Test')
Next, in this class we have defined its method named message(), using the keyword def.
In Python, every method/constructor of a class should be defined using a keyword self in its parameters, where self refers to the currently executing object of the class.
Definition of this method includes an indentedprint statement, used to print a message when this method is executed.
Note:All the statements in a method must begin with an indentation or a syntax error is reported.
ob = Test()
Next, outside the definition of a class, we have created an object named ob of class Test, by calling the constructor of the class, which always has the same name as the class.
ob.message()
Finally, using the dot operator . with the newly created object ob of class Test, we have called its method i.e. message() and its executed.
Advertisement
A class with methods and instance variables
In the upcoming example, we are going to create a class with multiple instance variables and a method.
The method would access these instance variables using the self keyword, which refers to the currently using object.
# Python - Creating a class with methods and instance variables
class Test2:
# Defining a method to set the values of instance variables associated with an object.
def set_values(self):
# instance variable, year
self.year = 2019
# instance variable, message
self. message = 'Have a good day!'
# Defining a method to print the values of instance variables associated with an object
def get_values(self):
print(self.message, self.year)
# Creating an object of class Test
ob = Test2()
# Calling the method set_values() to set the values of instance variables of an object
ob.set_values()
# Calling the method get_values() to get the values of instance variables of an object
ob.get_values()
Output
Have a good day! 2019
As you can see in the program, we have first created an object of the class Test and then we have set the values of
its instance variables using set_values() method and finally,
we have accessed the instance variables associated with an object from within the method named get_values().
Note: We have set and accessed the values of instance variables associated with an object
using the keyword self, which refers to the currently executing
object.
A skeletal class
In the upcoming example, we are going to create a skeletal class i.e. a class without any of its own methods or instance variables defined in it.
To create a skeletal class, we must use the pass keyword which allows us to pass the class definition.
# Python - A skeletal class without methods and instance variables
class Test:
pass
ob = Test()
print(ob)
Output
<__main__.Test object at 0x0000000002EBEEB8>
As you can see in the program, we have first created a skeletal class(without any methods or instance variables) using the pass keyword,
next we have created an object of this class and have got the representation of the object's address in the memory
by printing this newly created object of the class.