Classes in Python

Question

Differentiate between static and dynamic binding in Python? Give the suitable example of each.

Answer


Static Binding

Dynamic Binding

Time of binding

happens during compilation

binding happens at run time

Actual object

Actual object is not used for binding

Actual object is used for binding

Example

Method overloading

Method overriding

Methods of binding

Private, static and final methods show static binding. Because they cannot be overridden.

Other than private, static and final methods show dynamic binding. Because they can be overridden.

Class

Type class

Object class

Sponsor Area

Some More Questions From Classes in Python Chapter

Define a class RING in Python with following specifications

Instance Attributes
- RingID 		# Numeric value with a default value 101
- Radius 		# Numeric value with a default value 10
- Area 			# Numeric value
Methods:
- AreaCal() 	# Method to calculate Area as
				# 3.14*Radius*Radius
- NewRing() 	# Method to allow user to enter values of
				# RingID and Radius. It should also
				# Call AreaCal Method
- ViewRing() 	# Method to display all the Attributes 4

Differentiate between static and dynamic binding in Python? Give the suitable example of each.

Write two methods in Python using the concept of Function Overloading (Polymorphism) to perform the following operations:
(i) A function having one argument as side, to calculate Area of Square as side*side
(ii) A function having two arguments as Length and Breadth, to calculate Area of Rectangle as Length*Breadth.

class Exam:
	Regno=1
	Marks=75
	def __init__(self,r,m): #function 1
		self.Regno=r
		self.Marks=m
	def Assign(self,r,m): #function 2
		Regno = r
		Marks = m
	def Check(self): #function 3
		print self.Regno, self.Marks
		print Regno, Marks

(i) In the above class definition, both the functions - function 1 as well as function 2 have similar definition. How are they different in execution?

(ii) Write statements to execute function 1 and function 2.

Define a class BOX in Python with following specifications

Instance Attributes

- BoxID # Numeric value with a default value 101
- Side   # Numeric value with a default value 10
- Area  # Numeric value with a default value 0

Methods:

- ExecArea() # Method to calculate Area as
                  # Side * Side
- NewBox() # Method to allow user to enter values of
                # BoxID and Side. It should also
               # Call ExecArea Method
- ViewBox() # Method to display all the Attributes

Differentiate between static and dynamic binding in Python? Give suitable examples of each.

What is the significance of super() method? Give an example of the same.

How do we implement abstract method in python? Give an example for the same.