[NEWBIE] access return values

Peter Otten __peter__ at web.de
Thu Sep 18 06:45:21 EDT 2003


paul kölle wrote:

> 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




More information about the Python-list mailing list