Very simple - please help

P

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
 
C

Chris Rebert

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

The code you gave works perfectly:

chris ~ $ python
Python 2.5.1 (r251:54863, Feb 4 2008, 21:48:13)
[GCC 4.0.1 (Apple Inc. build 5465)] on darwin
Type "help", "copyright", "credits" or "license" for more information..... pass
....
commands = {1: (lambda: Blah())}
commands[1]()
[]

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
and you didn't import Blah into commands' module, hence why Python
can't find it. But we'd need some more details to be able to determine
that.

Cheers,
Chris
 
P

pineapple

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.
 
P

pineapple

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....
 
J

John Machin

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


Nope, that was the actual code - in fact it was the whole program.

You haven't shown *ALL* of what you think you typed. Here's a copy/
paste from my console:
=== start ===
C:\junk>ver

Microsoft Windows XP [Version 5.1.2600]

C:\junk>python
Python 2.5.2 (r252:60911, Feb 21 2008, 13:11:45) [MSC v.1310 32 bit
(Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information..... pass
....
commands = {1: (lambda: Blah())}
commands
{1: said:
commands[1]
commands[1]() []
cx = {42:Blah}
cx[42]() []

Now, what results do you get?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top