[NEWBIE] access return values

Discussion in 'Python' started by =?ISO-8859-1?Q?paul_k=F6lle?=, Sep 18, 2003.

  1. 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
    =?ISO-8859-1?Q?paul_k=F6lle?=, Sep 18, 2003
    #1
    1. Advertising

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

    Peter Otten Guest

    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
    Peter Otten, Sep 18, 2003
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Luhar Powell via .NET 247
    Replies:
    0
    Views:
    2,449
    Luhar Powell via .NET 247
    Apr 1, 2005
  2. Jim Benson

    newbie: suppressing return values

    Jim Benson, Dec 4, 2003, in forum: Python
    Replies:
    1
    Views:
    208
    Ben Finney
    Dec 4, 2003
  3. Greenhorn
    Replies:
    15
    Views:
    812
    Keith Thompson
    Mar 6, 2005
  4. Chris Rebert
    Replies:
    1
    Views:
    678
    Bobby
    May 28, 2009
  5. Luhar Powell via .NET 247

    Newbie:Access custom Itemplate(not datagrid/repeater/datalist) control values on postback

    Luhar Powell via .NET 247, Apr 1, 2005, in forum: ASP .Net Building Controls
    Replies:
    0
    Views:
    217
    Luhar Powell via .NET 247
    Apr 1, 2005
Loading...

Share This Page