Python: Can I have a string as a key and a function as value in a dictionary?
In 开发者_运维技巧other words, is this giving me errors purely because it's impossible to actually do or am I just writing it wrong:
class someClass:
self.myString = "Hello"
self.myMap = {
'someString' : self.functionInClass
}
So if functionInClass takes an argument, I want to call it like so from anywhere inside the class:
self.myMap[self.myString](theArgument)
This just yields:
NameError: name 'self' is not defined
Any help would be appreciated.
Yes you can do that, since methods (or bound methods) are just objects themselves.
In your code you forgot your function. I think you wanted to write somthing like this:
class someClass:
def __init__(self):
self.myString = "Hello"
self.myMap = {
'someString' : self.functionInClass
}
def functionInClass(self):
pass
This is perfectly valid.
>>> def f():
... print "test"
...
>>> d = {'df':f}
>>> d['df']()
test
>>>
This test code does not use a class, but same thing can be done with a class too. The error shows that self
is not defined, it has nothing to do with functions in dictionary.
There's nothing impossible about what you're trying to do:
class Test(object):
def func1(self, arg):
print 'func1, arg=%s' % arg
def func2(self, arg, **kwargs):
print 'func2, arg=%s, kwargs=%s' % (arg, kwargs)
funcmap = {'func1': func1, 'func2': func2}
def invoke(self, fname, *args, **kwargs):
Test.funcmap[fname](self, *args, **kwargs)
test = Test()
test.invoke('func1', 0)
test.invoke('func2', 42, kw=12)
Your code should work. Probably you just forgot something. This works pretty well for me:
class someClass:
def __init__(self):
self.myString = "someString"
self.myMap = {
'someString' : self.functionInClass
}
def functionInClass(self, argument):
print argument
def someMethod(self):
self.myMap[self.myString]("argument")
cl = someClass()
cl.someMethod()
cl.myMap[cl.myString]("argument")
If you wish for myMap
to be a class attribute, make the dict values the string-name for the methods and use getattr to retrieve the actual method:
class SomeClass(object):
myMap = {
'someString' : 'methodInClass'
}
def methodInClass(self,argument):
pass
x = SomeClass()
getattr(x,x.myMap['someString'])('argument')
精彩评论