Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > de.comp.lang.python > #5766

local variable referenced before assignment?

From Ulli Horlacher <framstag@rus.uni-stuttgart.de>
Newsgroups de.comp.lang.python
Subject local variable referenced before assignment?
Date 2021-11-17 15:14 +0000
Organization University of Stuttgart, FRG
Message-ID <sn3688$gpf$1@news2.informatik.uni-stuttgart.de> (permalink)

Show all headers | View raw


Ich bekomme diesen Fehler und verstehe ihn nicht:

    config()
  File "./fextasy.py", line 496, in config
    sg.I(server,key='server',tooltip='F*EX server',size=80)],
UnboundLocalError: local variable 'server' referenced before assignment


Der Sourcecode dazu ist:

def read_config():
  global server,user,aid,tmpdir,ddir
  server = user = aid = ''
(...)

def config():
  read_config()

  col = [
    [sg.I(server,key='server',tooltip='F*EX server',size=80)],


Die globale Variable server wird also in read_config() gesetzt.
Wieso wird die dann in config() als lokale variable behandelt?

https://docs.python.org/3/faq/programming.html#what-are-the-rules-for-local-and-global-variables-in-python

In Python, variables that are only referenced inside a function are
implicitly global.

-- 
Ullrich Horlacher              Server und Virtualisierung
Rechenzentrum TIK         
Universitaet Stuttgart         E-Mail: horlacher@tik.uni-stuttgart.de
Allmandring 30a                Tel:    ++49-711-68565868
70569 Stuttgart (Germany)      WWW:    http://www.tik.uni-stuttgart.de/

Back to de.comp.lang.python | Previous | NextNext in thread | Find similar


Thread

local variable referenced before assignment? Ulli Horlacher <framstag@rus.uni-stuttgart.de> - 2021-11-17 15:14 +0000
  Re: local variable referenced before assignment? Ulli Horlacher <framstag@rus.uni-stuttgart.de> - 2021-11-17 15:33 +0000

csiph-web