Very simple - please help

2008-11-07 Thread pineapple
I am not a python programmer, but am being forced to port one of my
(smalltalk) applications to python for pragmatic reasons (python is
embedded with a graphics package I am switching over to, so to use the
graphics package I am essentially forced to use python).  Okay, enough
background.

At any rate, in my smalltalk solution, in order to avoid an if-then-
else chain of "if this command, do this function, else if this command
do another function..." I have commands set up in a dictionary.  I
read the command integer, then key it into the dictionary to see what
method/function to call.

#Conceptual representation of dictionary with keys and values:

1: do_command1
2: do_command2
3: etc...

Trying to set up the same thing in python, it seems the lambda
expression is what I need.  So I set up a simple class to test this,
with some simple code as follows:

###
class Blah(list):
pass

commands = {1: (lambda: Blah())}
###

This is accepted by the interpreter, no problem.  If I type "commands"
into the interpreter I get the dictionary back showing the key '1'
attached to the lambda expression.  If I type "commands[1]" into the
interpreter, I get the lambda function back.  However, when I try to
invoke the lambda function with a "commands[1]()", I get a "global
name 'Blah' is not defined."  I find this error odd, because if I do a
"Blah()", I get back a "[]" as expected (a list).

For a day, I have tried everything under the sun I know to try.  For
instance, I thought perhaps lambdas don't work with methods, so I
wrapped the method call in a function.  But I get the same error.  I
have spent a day online googling this error, but have found nothing to
help me.

Can a guru out there help a python newbie with this?  I figure you can
spot the problem in, oh, 5 seconds or less?

Thanks
--
http://mail.python.org/mailman/listinfo/python-list


Re: Very simple - please help

2008-11-08 Thread pineapple
On Nov 8, 12:24 pm, "Chris Rebert" <[EMAIL PROTECTED]> wrote:
> The code you gave works perfectly:

Weird! Doesn't work at all on my system (WinXP, Python 2.5).

> Please post some of the actual code so that we can determine the problem.
> Taking a guess, I'd suspect Blah and commands are in different modules

Nope, that was the actual code - in fact it was the whole program.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Very simple - please help

2008-11-08 Thread pineapple
On Nov 8, 1:02 pm, "Mark Tolonen" <[EMAIL PROTECTED]> wrote:

> Also, you don't need a lambda for this example:

Interestingly, this works - thanks. I'd still like to know why the
other doesn't work, but I suppose at this juncture it isn't worth the
time and energy trying to figure it out
--
http://mail.python.org/mailman/listinfo/python-list