Using a dictionary to pass data to/from embedded python functions

Alex Martelli aleax at mac.com
Sat Aug 12 19:56:16 EDT 2006


wardm <wardm66 at gmail.com> wrote:

> Thanks Alex for your help, (and advice on focusing the point of my 
> question).
> 
> I was able to compile and run your example OK, but when I try to use the
> "VarDictionary" in the
> MyScriptModule.py code, I get an exception.
> 
> I  added the following code to the C app just to add two entries to the
> Dictionary
> 
>  PyDict_SetItemString( m_pVarDictionary, "tk1", 
> Py_BuildValue("s","test1Val"));
>  PyDict_SetItemString( m_pVarDictionary, "tk2", 
> Py_BuildValue("s","test2Val"));
> 
> Then tried various things in the Python code to display the contents of the
> "VarDictionary",
> such as adding the "print VarDictionary" below.
> 
> import InterfaceModule
> 
> def functionName():
>     print "hello"
>     print dir(InterfaceModule)
>     print "that's all"
>  print VarDictionary

Note the wrong indentation in this latter print statement: this would
already cause a syntax error (unless the leading 'p' happened to be
aligned with the leading 'd' of 'def', in which case the function would
be terminated, the latest print would happen at import-time, and the
FOLLOWING statement:

>     return

...would then be a syntax error (return outside of function).  But,
there's more:


> Even though "VarDictionary " is in the Dir, every time I try to use the
> "VarDictionary" the program fails.

"VarDictionary" is in the dir(...) *** of InterfaceModule ***, of
course, so you need to refer to it as InterfaceModule.VarDictionary in
your Python code -- the barename, nor qualified by modulename, just will
not work, of course!!!

Adding the two C code lines you quote, and changing the Python example
code to:

def functionName():
    print "hello"
    print dir(InterfaceModule)
    print "VarDictionary is:", InterfaceModule.VarDictionary
    print "that's all"

changes that part of the output to:

hello
['VarDictionary', '__doc__', '__name__']
VarDictionary is: {'tk2': 'test2Val', 'tk1': 'test1Val'} 
that's all


With all due respect, it looks like you're trying to run before you can
walk -- or more specifically, to embed Python in C++ before you become
familiar with the most elementary and fundamental aspects of Python,
such as indentation and the need to qualify compound names.  You might
want to consider getting a good Python book -- such as, my own Python in
A Nutshell (2nd ed), Aahz and Stef Maruch's Python For Dummies, Wesley
Chun's Core Python Programming (2nd ed) -- they're all very recent (mine
is just out, Aahz's and Stef's I believe is due to hit bookstores in
September or October), and any of them might serve you well (if you're
OK with books not necessarily covering the very latest release of Python
[and the issues you're having suggest that this is not really the
problem!], there are many other good books, such as Magnus Lie Hetland's
"Beginning Python", Beazley's "Python Essential Reference", Lutz and
Ascher's "Learning Python",  



More information about the Python-list mailing list