tkinter global variable
Cameron Simpson
cs at cskk.id.au
Fri Nov 13 18:56:14 EST 2020
On 13Nov2020 12:48, Dennis Lee Bieber <wlfraed at ix.netcom.com> wrote:
>On Fri, 13 Nov 2020 18:40:53 +0100, David KolovratnÃk
><david at kolovratnik.net> declaimed the following:
>>def on_printfilename():
>> global pic
>> try:
>> print( f"C: {pic}" )
>> except NameError:
>> print( f"C! pic not set yet" )
>>
>
> Just an aside: you only need "global <name>" if you are modifying the
>value bound to <name>. If <name> does not exist as a local and all one is
>doing is reading <name>, Python will automatically check the module
>(global) scope for that <name>.
This might be confusing< because that state is fragile, only applying if
"pic" is never set, only accessed.
As soon as David modifies on_printfilename() to _assign_ to pic, it
becomes a local name if he hasn't used "global".
IMO, if he must use "pic" as a global it is better to explicitly use
"global" anyway.
Cheers,
Cameron Simpson <cs at cskk.id.au>
More information about the Python-list
mailing list