passing function call as argument (metaprogramming)

This is the place for queries that don't fit in any of the other categories.

passing function call as argument (metaprogramming)

Postby krazygluon » Wed Apr 02, 2014 1:01 am

I've got a pygame application (read game) I'm working on that involves building some custom gui. I've got a main.py for constructing the menu screen and a menu.py for the menu and button classes.

in the main.py I implement the specific forms of the menu and button classes that will be in the menu, then the run loop in the menu class actually manages my pygame screen.

the buttons are designed to pass a function or other piece of python code to them as part of the arguments in their __init__()

Right now I'm able to pass basic functions via string between the main and any buttons I create from the main. (so my Quit Game button works just fine!)

The problem I'm running into is in trying to pass a command for one button to initiate another entirely new menu screen (a submenu) the command passed is the menu.run() method of my menu class

since both menus (main and submenu) were constructed in the menu file, the code being executed from the button class (which resides in a separate .py) doesn't know that the submenu class object even exists.

I tried to fix this by importing the main into the button class, but then I get some kind of recursion error right at startup where the first use of a button class in main breaks down because the two files import one another.

If this is too vague I'll see what I can do to put up some pseudocode or actual snippet later.

I'm thinking the solution might be to somehow make a custom instance of the class in my main so that the execution of the command passed to the button class is done from within the main (where both menu objects reside) instead of the menu module where the generic classes reside.

Any assistance is appreciated. I apologize for wretched language but my programming jargon is worse than my actual programming ability (self-taught)
krazygluon
 
Posts: 1
Joined: Tue Apr 01, 2014 6:08 am

Re: passing function call as argument (metaprogramming)

Postby micseydel » Wed Apr 02, 2014 1:20 am

Please read over this before posting again. The primary points (though you should still read it) are: always post code and tracebacks unless there's a really, really good reason not to, and use code tags when you do. And real code is preferable to pseudo-code nearly always as well, especially since Python is often close anyway, providing the abstractions that pseudo-code usually provides while being much more concrete.

Without the code it's somewhat difficult to say what the ideal solution is. When you ask about passing a function call around, here are the two thoughts that come my mind.
Code: Select all
>>> def f(x, y):
    return x + 2*y

>>> # i want to pass f(1, 2) to g()
>>> def g(wrapped_call):
    print wrapped_call()

    
>>> g(lambda: f(1, 2))
5
>>> 
>>> def g(function, args):
    return function(*args)

    
>>> print g(f, (1, 2))
5

The first creates a (lambda) function which will make the call I want, and the second passes the function and desired parameters which can then be combined later. They're functionally equivalent.

krazygluon wrote:I apologize for wretched language but my programming jargon is worse than my actual programming ability (self-taught)

Even more reason to post code ;)
Most if not all of us here are self-taught, and we've all been where you are, not knowing the jargon yet and having some goal in mind, wanting help, and not being 100% efficient at communicating those desires. But you're in the right place to learn all those things :)
Join the #python-forum IRC channel on irc.freenode.net!

Please do not PM members regarding questions which are meant to be discussed publicly. The point of the forum is so that others can benefit from it. We don't want to help you over PMs or emails.
User avatar
micseydel
 
Posts: 1222
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA


Return to General Coding Help

Who is online

Users browsing this forum: No registered users and 1 guest