Python tutorial : Classes and Objects part 2




Class Objects

When class is defined , the class name will act as a “class object”. This class object can be used to access the variables and functions defined in that class. These references can be made even before an instance of the class is created.

	
class phone:
	
	company = "Motorola"
	
	def get_info (self):
	
		print ("Phone company:", self.company)

print (phone.company)

phone.get_info(phone


In the above code, “class object” is “phone” , so phone.company and phone.call are valid references.



Functions are also objects in python

In python all functions are objects of the class function.

In a class there can be two types of attributes, data attributes and function attributes. The term “function object” is commonly used to refer to a function attribute of a class



Instantiation

An object is an instance of a class. Creating an object from a class is called as instantiation.

	
myphone1 = phone()


The above code creates an object “myphone1” of the class “phone”

Technically “myphone1” is an “instance object” created from the class “phone”

For example (assuming the above class):
	
myphone1 = phone ()

print (myphone1)


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

__main__.phone object at 0x7fd7dc668b38




Method and Method Object

Traditionally a function defined in a class is known as a method. Note that, though the term “method” is used when a function is defined inside a class , functions and methods are not synonymous.

A function associated with an object is known as method. All function objects defined in class will act as method for instances of that class.

In the above code “myphone1.call” is a method object while “phone.call” is a function object



Inheritance

Inheritance is when a class is created from another class or classes.

	
class phone:
	
	def call (self):
		print ("calling ...")
		 
class cellphone(phone):	

#this class will receive all the functions and variables of the class “phone”
	
	def message (self):
	
		print ("messaging...")


myphone1 = cellphone()

myphone1.message()

myphone1.call()


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

messaging...

calling ...


In the above code, class “cellphone” is a derived class of the base class “phone”. Even though “cellphone” class has only one function defined in it , it inherits the variables and functions defined in its base class.

So “myphone1.call()” and “myphone1.message()” are valid.

Inheritance reduces the coding required when similar classes with added functionalities are needed.

A class can inherit from multiple base class using following method:

class cellphone( Baseclass1, Baseclass2, Baseclass3….)


Data overriding and method overriding

When the same name is used for variables/functions in the derived class , they will overwrite the values and definitions given in the base class.

For example

	
class phone:

		number = 12345
		
		def call (self):
		
		 	print ("calling ...")
		 
class smartphone(phone):
	
		number = 56789
		
		def call(self):
		
			print ("calling from smartphone...")
			
		def message (self):
		
			print ("messaging...")

myphone1 = smartphone()


myphone1.call()

print ("smartphone number is",myphone1.number)


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

calling from smartphone...

smartphone number is : 56789




Private Variables

Variables which can be accessed only from inside an object is called as private variables. These variables can be created by adding two underscores before the variables name.

For example,
	
class phone:

	__number = 1234
	
	def call(self):
	
		print ("calling...", phone.__number)
		

myphone1 = phone ()

myphone1.call ()

print ("calling...", phone.__number)


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

calling... 1234

Traceback (most recent call last):

  File "oop.py", line 7, in 
	  
    print ("calling...", phone.__number)
    
AttributeError: type object 'phone' has no attribute '__number'



here “__number” is a private variable , “call” function can access this variable using phone.__number, but this will variable will not be available if you try to access the same from outside.



Next chapter: Python file handling

Previous chapter: Python training : Classes and Objects Part 1