Jump to content
  • Advertisement
Sign in to follow this  

Python: A dictionary of functions?

This topic is 4378 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Basicly what I'm trying to do is create a dictionary of functions so that I can call a function based upon a typed in command. The way I'm doing it right now is that I define a class for every command, and inside that class define a __call__ function in which I perform the actions I want. However, this seems sort of hackish. This is what I'm doing:
class Print_Class:
    def __init__(self):
    def __call__(self):
        print "Print was Called"
and then I do this:
class Token:
    done = False
    var = "f"
    Print_Command = Print_Class()
    Call_Command = Call_Class()
    Pi_Command = Pi_Class()
    command_dict = {}
    command_dict['Print'] = Print_Command
    command_dict['Call'] = Call_Command
    command_dict['Pi'] = Pi_Command
and when I call the function I do this: self.command_dict[self.var]() Is there any other way of doing this? Basicly, something like like an std::map<string, void (*funcptr)()>//Not sure on the syntax on that in C++, or am I doing it the right way?

Share this post

Link to post
Share on other sites
It sounds like what you want to do is use a dictionary of unbound (non-method) functions instead.

def Print_Command():
# ...

def Call_Command():
# ...

def Pi_Command():
# ...

function_dict = {
"Print": Print_Command,
"Call": Call_Command,
"Pi": Pi_Command

# to execute any of these functions, simply do:
token = "Print"

To execute every function in alphabetical order, try:
commands = function_dict.keys()
for command in commands:

Share this post

Link to post
Share on other sites
Thanks, it worked.

EDIT: I couldn't get the dictionary to accept the functions before, which is why I switched to the class method. Apparently Python is quite picky [grin]

Share this post

Link to post
Share on other sites
Tac-tics is correct. You have to keep in mind that *everything* in Python is an object, including plain simple functions.

However, I don't think the first solution has anything particularly "hackish" in it, it could be useful in other situations.

Share this post

Link to post
Share on other sites
Original post by SticksandStones
Apparently Python is quite picky [grin]

I don't think Python's that picky, but it can be a bit tricky.

Despite it's very precise, easy-going appearance, when it comes to how classes and functions as objects work, you can easily fall into some problems.

Always check your parenthesises with functions. Normally, if you have

def do():
print "Do!"

# works as expected, prints "Do!" to the STDOUT

# uh oh! Returns a function object, prints nothing to the screen.

# Sometimes it can be even nastier...
def get_some_primes():
return (2,3,5,7,11,13)

# primes becomes the tuple: (2,3,5,7,11,13)
primes = get_some_primes()

# primes is *not* a a tuple of primes, it becomes a function.
primes = get_some_primes

# TypeError, unsubscribable object!
primes [0]

Classes can be just as mean, because, although writting a class requires relatively few keywords, Python has more "behind the scenes" magic going on than even Java. I remember one particular trouble I had early on was with the __getattr__ attribute, which makes creating infinite loops easy as pie.

Share this post

Link to post
Share on other sites
You might want to have a look at the cmd module. shlex and asynchat may also be useful, depending on what you're doing.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!