Python OOPs Concepts 2

Classes in Python

In this topic, we will discuss how to define a class in Python. As discussed classes are a collection of variables, and functions, that are bound together.

The syntax for defining a class

Defining a class is simple, all you have to do is use the keyword class followed by the name that you want to give your class, and then a colon symbol “:”(colon). It is a standard approach to start the name of the class with a capital letter and then follow the camel case style.

The class definition is included, starting from the next line and it should be indented, as shown in the code below. Also, a class can have variables and member functions in it.


class MyClass:
    # member variables
	variable1 = something
	variable2 = something
	# member functions
	def function1(self, parameter1, ...):
		self.variable1 = something
		# defining a new variable 
		self.variable3 = something
		function1 statements...
	def function2(self, parameter1, ...):
		self.variable2 = something
		function2 statements...

Lets discuss the code above, we have used the keyword class, to inform the compiler that from here a new class definition starts, followed by the name of the class i.e., MyClass.

In Line 2 we have added a comment and in line 3 and 4, we declared two variables variable1 and variable2 and have also initialized them with some values.

This is just a sample to explain how we define a class, we will see an example for class, after this section.
Further below there are two functions defined. Again, we have mentioned sample functions just to explain the syntax.
As you can see in the code, it’s pretty much the usual function definition like we do without a class, using the “def” keyword, except for a new parameter named “self”. This is what makes class’s function different from the usual function.

It’s a mandatory parameter for every function defined in a class. “self” represents the current active object of the class, using which the function of the class is called. Don’t worry if you are not able to understand it, we will explain this again when we will learn about objects.

This was how we define a class in python, now let’s understand how to create an instance/object of a class.

Creating object for a class

Class is mere a blueprint or a template. No storage is assigned when we define a class. Objects are instances of class, which holds the data variables declared in the class and the member functions work on these class objects.

To create an object, all we have to do is:

myObject = MyClass()

where, MyClass is the name of the class and myObject is the object variable.

As you can see, we simply specified the object’s name and we called a function which had the same name as of the class to which it belongs.

Similar is the case here, we have created a user-defined data type(a class) called MyClass, and in order to inform python that myObject variable will be of that datatype, we make a call to this function.

Few points to note:

* We add the self parameter when we define a member function, but do not specify it while calling the function.
* To access a member function or a member variable using an object, we use a dot . symbol.
* And to create an object of any class, we have to call the function with same name as of the class.