The class in Python in a general form can be written as
As seen from the general form of the class definition the class body should be indented. To indent the class body use four spaces. The general form of creating objects for a class can be written as:
Example 1 Create a class named Cars and in the class body type pass keyword. Then create the objects "Mustang", and "Viper" of the class Cars.
Solution:The class definition must start with class keyword followed by the class name which is in this case Cars followed by the colon. The class body is indented i.e. four spaces and in this case, it contains the pass keyword.
class ClassName:In previous code block the keyword class is used to define a class. The class keyword is followed by the name of the class (ClassName) which is used to identify the class. The definition of the class is terminated with a colon. So, the class definition starts with keyword class followed by the class name (identification of a class) and is terminated with a colon.
"Class Body"
Important note: The class name should follow variable naming rules which means it must start with a letter/underscore and can be comprised of letters, underscores or numbers. The class should be named using CamelCase notation i.e. each word starts with capital letter.
As seen from the general form of the class definition the class body should be indented. To indent the class body use four spaces. The general form of creating objects for a class can be written as:
object_name = ClassName()This code instantiate object from the class which is named object_name. The instance of the class is created with the class name followed by parenthesis which is similar to a function call.
Example 1 Create a class named Cars and in the class body type pass keyword. Then create the objects "Mustang", and "Viper" of the class Cars.
Solution:The class definition must start with class keyword followed by the class name which is in this case Cars followed by the colon. The class body is indented i.e. four spaces and in this case, it contains the pass keyword.
class Cars:When we run this code nothing will happen. The next step is to instantiate two objects "Mustang" and "Viper". To instantiate two objects from the class Cars type in the following code.
pass
Mustang = Cars()This code instantiates two objects of the new class i.e. "Mustang" and "Viper" by creating two instances of a class "Cars" followed by parentheses. To show some output we will use the print method to print out both objects.
Viper = Cars()
print("Mustang = {}".format(Msutang))The entire code used in this example as well as the output are given below. The code used in Example 1.
print("Viper = {}".format(Viper))
class Cars:The output in Example 1.
pass
Mustang = Cars()
Viper = Cars()
print("Mustang = {}".format(Mustang))
print("Viper = {}".format(Viper))
Mustang = <__main__.Cars object at 0x0000022BAAF51520>The output showed which class those two objects belong to and at which memory addresses are they located. From memory, adresses ti can be noticed that each object is stored at different memory addresses.
Viper = <__main__.Cars object at 0x0000022BAAF51A90>