Python tutorial : Classes and Objects




Before we begin with Object oriented programming , I would like to note that this topic is divided in to two parts. First part contains some simple examples of classes, objects, object variables, class variables, init function and some OOP terminologies. Second part of this tutorial contains OOP features like inheritance, private variables, class objects, member objects, function objects etc

It will be a good idea to go through the concepts of functions and scope of variables (local and global variables) before you start this tutorial.


Classes as a collection of variables


class phone:
	
	company = "Motorola"
	
	model 	= "Moto X"
	
	price	= 15000
	
	
myphone1 = phone()

print (myphone1.company)

print (myphone1.model)

print (myphone1.price)


-----------Output----------

Motorola

Moto X

15000


In the above program, class “phone” is used just as wrapper for grouping three variables. The line “myphone1 = phone()” creates an object “myphone1” which can be used to access the variables using the syntax “objectName.Variable”

Here we are using a class to keep a list of variables under one name. This is similar to composite data type “struct” used in C programming.



Class as a collection of functions

	
class phone:
		
	def set_info (self, model, number):
		
		self.model = model
		
		self.number = number
	
	def get_info (self):
	
		print ("Phone model is", self.model)
		
		print ("Phone number is ", self.number))

myphone1 = phone()

myphone1.set_info("Moto X",12345)

myphone1.get_info()


-----------Output----------

Phone model is Moto X

Phone number is  12345


Here class “phone” is a set of two functions "set_info" & "get_info" , to invoke these functions syntax is "objName.function()". These functions also holds some variables which are accessed in the same way.



Defining Classes and Objects

Few ways to define Classes
- A Class is a way of grouping variables and related functions which act as a template to create objects.
- A Class is a model or blueprint or plan to create an object
- A Class act a user defined data type used to create its instances known as objects
Few ways to define Objects
- If class is representing a model or template, the product that we build using the class is known as an object.
- An object is a collection of variables and functions
- An instance created from a class is called as an object

The variables and functions of a class are also known as data attributes and function attributes.



Argument “SELF”

Every function defined inside a class will have argument “self” which will be replaced with the instance object. While calling the function this object is inserted as the first argument automatically, so this argument is not mentioned by user during function call.

In the above mentioned code, function definition of “set_info” has three arguments “self” , “model”and “number” but while calling the function only two arguments are given,

“model” is assigned with “Moto X”

“number” is assigned with “12345"

“self” is assigned with “myphone1” which is an instance object (Instance objects are explained later)

Note: “self” is not a keyword , though it is a common practice to use “self” , you can use any name instead of self.



Local variables and Object variables

	
def set_info (self, model, number):

	self.model = model
	
	self.number = number
	

In the above function , arguments model and number are local variables of the function set_info. The scope of these variables are with in the function and will not available in other functions or outside the class.



Object variables or Instance variables

These are variables which are associated to each object

In the variable name “self.model” , "self" represents instance object (object name), in this example it is “myphone1”. If multiple objects are created , for each object they will have their own unique object variables.

For example , if you add the following code in the above program , "myphone1.number" and "myphone2.number" are representing two different variables.

	

myphone2 = phone ()

myphone2.set_info ("Moto Y", 56789)

Object variables will be available in all class functions as well as outside.

Checkout the second function get_info, this function is accessing variables created in the function “set_info”

		
def get_info (self):

	print ("Phone model is", self.model)
	
	print ("Phone number is ", self.number)

Following code shows how to access object variables from outside

	
class phone:

	def set_info (self, model, number):
	
		self.model = model
		
		self.number = number
	
	def get_info(self):
	
		print ("Phone model is", self.model)
		
		print ("Phone number is ", self.number)

	
	
myphone1 = phone ()

myphone1.set_info ("Moto X", 12345)

print ("Accessing phone model from outside:", myphone1.model)

#changing phone model
myphone1.model = "Moto Y"

myphone1.get_info()


-----------Output----------

Accessing phone model from outside: Moto X

Phone model is Moto Y

Phone number is  12345




Class variables

Class variables are shared by all objects of the same class.


class phone:

	company = "Motorola"
	
	count = 0

	def set_info(self,model,number):
	
		self.model = model
		
		self.number = number
		
		phone.count += 1
	
	
	
myphone1 = phone ()

myphone2 = phone ()

myphone1.set_info ("Moto X", 12345)

myphone2.set_info ("Moto Y", 56789)

print ("No: of phones: ", phone.count)

print ("Phone1 company: ", myphone1.company)
print ("Phone2 company: ", myphone2.company)


-----------Output----------

No: of phones:  2

Phone1 company:  Motorola

Phone2 company:  Motorola


Note: A class variable can be accessed using either "className.variable" or "objName.variable".



__init__ function:

If a class has defined a function with name “__init__ ” , it will be invoked automatically when a object is created. Argument for this “__init__ ” is passed along with class name when the object is created.

Checkout the following example where the “set_info” function is renamed to “__init__”

	
class phone:
	
	def __init__ (self, model, number):
	
		self.model = model
		
		self.number = number
			
	def get_info (self):
	
		print ("Phone model is", self.model)
		
		print ("Phone number is ", self.number)

	
myphone1 = phone ("Moto X",1 2345)

myphone2 = phone ("Moto Y", 56789)

myphone1.get_info ()
myphone2.get_info ()


-----------Output----------

Phone model is Moto X

Phone number is  12345

Phone model is Moto Y

Phone number is  56789




Next chapter: Python training : Classes and Objects Part 2

Previous chapter: Python training : Functions, Lambda functions, Local and Global variables