Hamdy Elgammal
Hamdy Elgammal

Reputation: 73

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.

Upvotes: 1

Views: 164

Answers (5)

unutbu
unutbu

Reputation: 880547

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')

Upvotes: 0

uaraven
uaraven

Reputation: 1107

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")

Upvotes: 0

NPE
NPE

Reputation: 500773

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)

Upvotes: 0

taskinoor
taskinoor

Reputation: 46037

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.

Upvotes: 1

Constantinius
Constantinius

Reputation: 35069

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

Upvotes: 3

Related Questions