Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

local variable referenced before assignment?

9 views
Skip to first unread message

Ulli Horlacher

unread,
Nov 17, 2021, 10:14:17 AM11/17/21
to
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: horl...@tik.uni-stuttgart.de
Allmandring 30a Tel: ++49-711-68565868
70569 Stuttgart (Germany) WWW: http://www.tik.uni-stuttgart.de/

Ulli Horlacher

unread,
Nov 17, 2021, 10:33:48 AM11/17/21
to
Ulli Horlacher <fram...@rus.uni-stuttgart.de> wrote:
> 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
>
>
> def config():
> read_config()
>
> col = [
> [sg.I(server,key='server',tooltip='F*EX server',size=80)],

Habs... :-}

Weiter unten im Code hatte ich noch ein ueberfluessiges
server = ...
stehen.

Was ich daraus gelernt habe:
Der Python Parser schaut sich das zur compile-time an, nicht zur Laufzeit.
0 new messages