HELP:UnboundLocalError: local variable '_nntp' referenced before assignment

Discussion in 'Python' started by Peter Moscatt, Mar 18, 2005.

  1. Hi all,

    I am in the process of writing an app that will handle news feeds and
    therefore using the 'nntplib'

    When I issue the connect command all goes will but when I issue the 'quit()'
    command I get the following error message:


    Exception in Tkinter callback
    Traceback (most recent call last):
    File "/usr/lib/python2.3/lib-tk/Tkinter.py", line 1345, in __call__
    return self.func(*args)
    File "./pnews.py", line 26, in callconnect
    _nntp.quit()
    UnboundLocalError: local variable '_nntp' referenced before assignment


    The code below illustrates how I'm using the code. Can anyone help me out
    in getting this right.

    Pete


    def callconnect():
    if b["text"]=="Connect":
    _nntp =
    nntplib.NNTP(_global.servername,int(_global.portnumber),_global.userid,_global.userpassword)
    if(_nntp):
    b["text"]="Disconnect"

    elif b["text"]=="Disconnect":
    _nntp.quit()
     
    Peter Moscatt, Mar 18, 2005
    #1
    1. Advertising

  2. Peter Moscatt wrote:
    > UnboundLocalError: local variable '_nntp' referenced before assignment


    This pretty much says what your problem is: you haven't a variable called
    _nntp

    > def callconnect():
    > if b["text"]=="Connect":
    > _nntp =
    >

    nntplib.NNTP(_global.servername,int(_global.portnumber),_global.userid,_global.userpassword)
    > if(_nntp):
    > b["text"]="Disconnect"
    >
    > elif b["text"]=="Disconnect":
    > _nntp.quit()


    And here we see why: In the Disconnect-case, where is that _nntp supposed to
    come from? I'm not sure what you want here, as you seem to rely on global
    variables very much, but to me the whole elif-block is bogus. You
    unecessarily communicate over b['text']

    Do it like this:

    def callconnect():
            if b["text"]=="Connect":
                    _nntp =
    nntplib.NNTP(_global.servername,int(_global.portnumber),_global.userid,_global.userpassword)
                    if(_nntp):
                    _nntp.quit()


    --
    Regards,

    Diez B. Roggisch
     
    Diez B. Roggisch, Mar 18, 2005
    #2
    1. Advertising

  3. Diez B. Roggisch wrote:

    > Peter Moscatt wrote:
    >> UnboundLocalError: local variable '_nntp' referenced before assignment

    >
    > This pretty much says what your problem is: you haven't a variable called
    > _nntp
    >
    >> def callconnect():
    >> if b["text"]=="Connect":
    >> _nntp =
    >>

    >

    nntplib.NNTP(_global.servername,int(_global.portnumber),_global.userid,_global.userpassword)
    >> if(_nntp):
    >> b["text"]="Disconnect"
    >>
    >> elif b["text"]=="Disconnect":
    >> _nntp.quit()

    >
    > And here we see why: In the Disconnect-case, where is that _nntp supposed
    > to come from? I'm not sure what you want here, as you seem to rely on
    > global variables very much, but to me the whole elif-block is bogus. You
    > unecessarily communicate over b['text']
    >
    > Do it like this:
    >
    > def callconnect():
    > if b["text"]=="Connect":
    > _nntp =
    >

    nntplib.NNTP(_global.servername,int(_global.portnumber),_global.userid,_global.userpassword)
    > if(_nntp):
    > _nntp.quit()
    >
    >


    G'Day Diez,

    Thanks mate.... yes, that did the trick. I guess why I am using those
    globals is because I really need them to be seen by other modules.

    Thanks again for the help.

    Pete
     
    Peter Moscatt, Mar 19, 2005
    #3
    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. Replies:
    2
    Views:
    5,328
    Diez B. Roggisch
    Feb 26, 2006
  2. Wang, Harry
    Replies:
    2
    Views:
    323
  3. Gabriel Rossetti
    Replies:
    3
    Views:
    987
    Hrvoje Niksic
    Dec 9, 2009
  4. ch1zra
    Replies:
    9
    Views:
    3,141
    danieldelay
    Jun 8, 2010
  5. Replies:
    9
    Views:
    102
    John Gordon
    Jun 24, 2013
Loading...

Share This Page