[Tutor] Question on Bound & Unbound methods

K G K G" <learnpython@rediffmail.com
Fri May 16 02:50:07 2003


Hi All,

Please correct me.

class Base:
 	def func(self):
 		return "func"
 	def afunc():
 		return "afunc"

Now even though "afunc" features in Base's __dict__, am not able 
to call the function. Even though am able to acquire a handle to 
afunc am helpless. I wonder if it makes sense to have such a 
function
defined in a class. I guess we have to use staticmethod() in case 
we want to define a static method?? bcos that is the effect i was 
lookign for when i defined afunc().

Coming to bound & unbound methods,

1. they necessarily have to be instance methods (ie something that 
has 'self' as it's first argument).
2. The same method is supposed to be "bound" if it's called using 
an instance and unbound if called using the class object except 
that instance needs to be passed explicitly in the latter case.

Now when w'd i use a unbound method?

1. If i have to call super class's methods

class Derived(Base):
    def func(self):
       Base.func(self)

2. If we have a instance variable and instance method name to be 
the same, then the only way am able to access the method is by 
using unbound methods.

class Test:
  def__init__(self):
   self.name="python"
  def name(self):
   return "Test"

t = Test()
t.name #python
t.name() # Flags an error: str not callable
Test.name(t) # Works

Are there any other situations where unbound methods are 
applicable?

thanks!
karthik.








___________________________________________________
Get email that means BUSINESS! me @ mycompany.com.
Just Rs.1499/year.
To start, click http://www.rediffmailpro.com