← Back to team overview

yade-users team mailing list archive

Re: [Question #695676]: How can I use list defined outside, inside the function

 

Question #695676 on Yade changed:
https://answers.launchpad.net/yade/+question/695676

Jan Stránský posted a new comment:
the use of global is OK in the example (inside a function, before
accessing global variable), although it is not necessary in this "read"
case. global would be needed in case of assigning to a global variable.

Example (in pure python):

### not assigning
l = [1,2,3]
def trail():
    l.append(4) # no need of global
trail()
print(l) # [1,2,3,4]
###

### assigning
l = [1,2,3]
def trail():
    l = [4,5,6] # l is local to trail, different from global l
trail()
print(l) # [1,2,3]
#
def trail():
    global l
    l = [4,5,6] # now l is the global l
trail()
print(l) # [4,5,6]
###

cheers
Jan

-- 
You received this question notification because your team yade-users is
an answer contact for Yade.