Click here to Skip to main content
15,886,799 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello Friends,
I defined a class like below


class Robot:
	def func(self):
		self.Num = 20


on the shell i typed:
>>> print Robot.func


So i got the output like below:
<unbound method Robot.func>



Then i again typed:
>>> r = Robot()
  >> r.func.im_self.func


This time i got:
<bound method Robot.func of <__main__.Robot instance at 0x0000000002F81E48>>


If you see the outputs, you will notice a "Bound" and an "Unbound" keyword.
What is the difference between the 2?

Thanks and Regards,
Rahul
Posted

1 solution

When you call func on the class itself - Robot.func - you got a unbound object automatically created by Python...It actually means that you are executing a method without any bound to any type/instance (it is a change between 2.x and 3.x -in 2.x there was a type checking even for this type of method call...).
When you call the method on an instance you get a method call that bounded to that instance. Python translate your call of r.func to Robot.func(r), where r is the instance. In this case there is a type checking (even in 3.x) to ensure that the function and the instance are match...
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900