passing variables from pyshell to wxpython app

hello, I'm still new at this python stuff so I'm not sure I understand.
I have the pyshell embedded into my wxpython application. I want to
access the same variable from either the pyshell or my app. right now I
use pass the variables to the pyshell but when I operate on them they
don't change in my application. so I think I need to pass the variables
back or somehow pass teh pointer from my app to the pyshell so that
when I operate on a variable it also changes in my application. I appreciate
the help.

Let us imagine that in your application you have a variable x.

    x = ...

Let us imagine that you want your embedded interpreter to have access to
x.

    pyWindow1.interp.locals['x'] = x

Now, if in the interpreter you later say...

    x = ...

That won't change the value of x as seen by your application. Why? For
the same reason that...

def foo(x):

... x = 7
...

def bar():

... x = 8
... print x
... foo(x)
... print x
...

bar()

8
8

...makes sense. If you want changes to some object to be seen in both
the shell AND in your application, you are either going to need to give
the shell access to your application's internals (probably not suggested):

    import __main__
    pyWindow1.interp.locals = __main__.__dict__

Or you are going to need to pass mutables and modify them. Here is an
example.

In your application:

    class Foo:
        pass

    mutable1 =
    mutable2 = {}
    mutable3 = Foo())

    pyWindow1.interp.locals['mutable1'] = mutable1
    pyWindow1.interp.locals['mutable2'] = mutable2
    pyWindow1.interp.locals['mutable3'] = mutable3

If you do the following in the shell:

    mutable1.append('value')
    mutable2['key'] = 'value'
    mutable3.attribute = 'value'

Those changes will be seen in your application, because they are
modifying objects which are mutable, and which are also seen in your
application.

While I understand the utility in embedding an interactive Python shell
into an application, I would be remiss if I didn't tell you that such
things are dangerous to provide in software which is to be released,
unless the point of the software is to be used as an interactive Python
shell, and not much else.

- Josiah

ยทยทยท

Jeff Peery <jeffpeery@yahoo.com> wrote:

Josiah Carlson <jcarlson@uci.edu> wrote:

Jeff Peery wrote:
> hello, I'm using the following command to push variable from my
> wxpython application into the pyshell.
>
> #create python shell and editor
> pyWindow1 = py.shell.Shell(parent, -1, introText = None)
>
> #put variables into shell
> pyWindow1.interp.locals['partList'] = py.shell.ShellFacade(partList)
>
> how do I push variables back from the pyshell to my application? I
> want to directy operate on my variable from the pyshell. so I'm passing
> them to the pyshell, operating on them, then I want to pass them back.
> whats the best way to do this?

Just make sure that the objects you want to modify have a variable in
whatever module you import.

>From the pyshell:

import __main__
__main__.attr = value

- Josiah

---------------------------------
Yahoo! FareChase - Search multiple travel sites in one click.