iamusingalistonwhichsomefunctionsworksinmyprogram.thisisasharedlistactuallyandallofmyfunctionscaneditit.isitreallynecessararation
imeanputtingtheglobalkeywordbehinditineachfunctionthatusesit,ordefiningitoutsideofallthefunctionsisenoughwithoutusingtheglobobeglobusit
whenyouassignavariable(x=. )、 youarecreatingavariableinthecurrentscope (e.g.localtothecurrentfunction ).ifithappenstoshadowavariablefronanouter ).e ) ) scope,welltoobad-python doesn ' TC are (and that ' sagoodthing ).So you can't do this:
x=0
杰夫(: )
x=1
f () ) )
打印x #=0
and expect 1. Instead,youneeddodeclarethatyouintendtousetheglobalx :
x=0
杰夫(: )
全局x
x=1
f () ) )
打印x #=1
utnotethatassignmentofavariableisverydifferentfrommethodcalls.youcanalwayscallmethodsonanythinginscope-e.g.onvariariabler
alsoveryimportant 3360 member assignment (x.name=. ),itemassignment ) collection[key]=.sliceassignment ) sliciciction andthereforeyoudon ' tneedglobaltochangeaglobal ' smembersorcallitmethods (evenwhentheymutatetheobject )。