great that is what i looked for.
>>class C:
... def function(self, arg):
... print arg
...
>>obj = C()
>>d = C.__dict__
>>d['function'](obj, 42)
42
this allows me the access the same method in a range of objects.
i can put all the functions i need in a dictionary as items, and the
vars as keys, and then call them for all objects that belong to a
class..
something like this
----------------------------------------------------
class C:
#object vars
self.my_voice
self.my_size
self.my_feel
# a method that do somthing, that might give different result for
different objects
getVoice(self):
return(self.my_voice+'WOW')
getSize(self):
return(self.my_size*100)
getFeel(self):
return(self.my_feel)
#create the dictionary with a reference to the class methode
dic={'voice':C.getVoice,'size':C.getSize,'feel':C. getFeel}
# create array of 10 different objects
cArray = []
for i in range(10)
cArray.append(C())
cArray[0].my_size=i
# choose the function you need, and get the result
choice=WHAT EVER KEY (e.g 'size')
for i in range(10)
print dic[choice](cArray[i])
#or even print all the values of all objects. if i ever want to print
diffenet valuse i only need
# to change the dictionary, nothing else...
for choice in dic:
for i in range(10)
print dic[choice](cArray[i])
---------------------------------------------------------------
i totaly forget about the "self" argument in every method...
a. is the main reason "self is there, or is it only a side effect?
b. what do you think about this code style? it is not very OOP, but i
cant see how one can do it other wise, and be able to control the
function printed out with something as easy as dictionary..
Georg Brandl wrote:
noro wrote:
Is it possible to do the following:
for a certain class:
----------------------------
class C:
def func1(self):
pass
def func2(self):
pass
def func4(self):
pass
obj=C()
----------------------------
by some way create a dictionary that look somthing like that:
d= {'function one': <reference to C.func1()>, \
'function two': <reference to C.func2()>, \
'function three': <reference to C.func3()>}
Perhaps this:
>>class C:
... def function(self, arg):
... print arg
...
>>obj = C()
>>d = C.__dict__
>>d['function'](obj, 42)
42
>>>
Georg