[Tutor] Adding a bound method dynamically (newbie)
karthik Guru
karthikg@aztec.soft.net
Fri, 23 Nov 2001 13:42:40 +0530
hi all,
say i have a class
class Test:
def __init__(self):
self.name="Python"
self.age=100
def func(self):
print 'func'
Now in another script i want to do
def boundMethod():
//s'd've access to name and age i defined earlier
and then i w'd like to do
t = Test()
t.salary = 10000
and then
t.func2 = boundMethod
This does'nt work because boundMethod does'nt've an access to the "self"
Basically i want this call to work
t.func2(). I don't want to do t.func2(t) and thereby giving access to the
instance.
Another question:
when i did a dir(<class-name>) => it gave a list of the methods associated
with the class irrespective of wether it is a bound method or not.
when i did a dir(<instance of class>) => it gave a list of the instance
attributes associated with the class
Now i might have methods which are bound so s'd'nt dir(<instance>) indicate
that those bound methods can also be invoked on the instance? Basically i
w'd like to know as to why dir() behaves that way.
Then there's a vars() :-(...confusing!
I remeber having coming across something like vars() looks for __members__
...am not sure what that stands for.
l =[]
dir(l) => lists all the instance method of l. I want to give that kind of a
functionality to my class instance as well.
what s'd i be doing?.
Hope i have managed to convey my queries in a proper way.
thanks in advance,
karhik.