[NEWBIE] access return values

  • Thread starter =?ISO-8859-1?Q?paul_k=F6lle?=
  • Start date
?

=?ISO-8859-1?Q?paul_k=F6lle?=

hi folks,

during the last couple of days I wrote my first small app with wxPython
and it all works pretty well, but now I'm somewhat lost. I'd like to
access the return value from a function inside an imported module (a
method of a class of that module eh?)

---gui.py----

from wxPython.wx import *
from app.py import *

user=UserObject(config) # class instance from app.py

the class UserObject has a function set_pwd() which returns a var, say
"ch_success" indicating success/failure wich I'd like to pass to a
Dialog to give some feedback to the user.

but:
--in gui.py----

d=wxMessageDialog(self, user.ch_success,"Info:", wxICON_INFORMATION | wxOK)

gives:

AttributeError: UserObject instance has no attribute 'ch_success'

That makes sense, since ch_success is a local variable of set_pwd(), so
I tried to make it global but with very little success so far. I tried:

--in app.py----

def set_pwd(someargs, moreargs)
do stuff //
...
global ch_success
or:
return ch_success
or:
self.ch_success

Nothing worked (but frankly, I don't really know what I'm doing here).

Any ideas? Thanks

Paul
 
P

Peter Otten

paul said:
hi folks,

during the last couple of days I wrote my first small app with wxPython
and it all works pretty well, but now I'm somewhat lost. I'd like to
access the return value from a function inside an imported module (a
method of a class of that module eh?)

---gui.py----

from wxPython.wx import *
from app.py import *

user=UserObject(config) # class instance from app.py

the class UserObject has a function set_pwd() which returns a var, say
"ch_success" indicating success/failure wich I'd like to pass to a
Dialog to give some feedback to the user.

but:
--in gui.py----

d=wxMessageDialog(self, user.ch_success,"Info:", wxICON_INFORMATION |
wxOK)

gives:

AttributeError: UserObject instance has no attribute 'ch_success'

That makes sense, since ch_success is a local variable of set_pwd(), so
I tried to make it global but with very little success so far. I tried:

--in app.py----

def set_pwd(someargs, moreargs)
do stuff //
...
global ch_success
or:
return ch_success
or:
self.ch_success

Nothing worked (but frankly, I don't really know what I'm doing here).

Any ideas? Thanks

Paul

A function does not keep its return value between calls. You have to bind it
like so:

func(args) # return value lost forever
value = func(args) #keep it in value

and can use value in subsequent code. I can only guess that set_pwd() is a
method of UserObject and ch_success an attribute:

class UserObject:
set_pwd(self, pwd):
# your code
self.ch_success = True # say, the pwd was successfully set

which would be used:

user = UserObject()
user.set_pwd("secret")
print user.ch_success

Please post *real* code and the resulting tracebacks to allow for a more
detailed analysis.

Peter
 

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

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top