How can I use list defined outside, inside the function

Asked by Trishala DAKA on 2021-02-21

Hi,

I tried using global in order to use the list defined inside the function,but yade shows error name " " is not defined.
>l = [1,2,3]
>def trail():
   >...: global l
   >...: e = len(l)
   >...: trail()

ERROR SHOW IN YADE
/usr/bin/yade in <module>
      2 global l
      3 e = len(l)
----> 4 trail()

/usr/bin/yade in trail()
      1 def trail():
      2 global l
----> 3 e = len(l)
      4 trail()

I also tried with saveVars I am getting the same error.
Can any one suggest the right way to use the list I defined inside a function.

Regards,
SaiDaka

Question information

Language:
English Edit question
Status:
Solved
For:
Yade Edit question
Assignee:
No assignee Edit question
Solved by:
Trishala DAKA
Solved:
2021-02-22
Last query:
2021-02-22
Last reply:
2021-02-21
Jan Stránský (honzik) said : #1

Hello,

> Can any one suggest the right way to use the list I defined inside a function.

it pretty much depends on context and what "use" does mean.
- do you call it in PyRunner?
- do you only read the list, or also modify?
- do you use it in a script or directly in terminal?
- ... ?

> ERROR SHOW IN YADE

please show the complete error [1] (not just lines, but primarily the error :-)

Also please post a MWE [1], W = working = reproducing your problem (here you just define the function, you do not show how you call it - or at least it is not clear).

With this script, I have no problem:
###
l = [1,2,3]
def trail():
    print(len(l))
trail()
###

BUT, copy-pasting this line-by-line to Yade terminal shows "NameError: global name 'l' is not defined".
However, using scripts is "more standard" than working directly in the terminal, I **personally** use it just for some quick tests, using it for situations like this, where namespaces are involved, sometimes needs some hacks, e.g.:
### python 2
import __builtin__
__builtin__.l = l
trail() # OK
###

### python 3
import builtins
builtins.l = l
trail() # OK
###

putting the code in a script is IMO more convenient.

cheers
Jan

[1] https://www.yade-dem.org/wiki/Howtoask

Robert Caulk (rcaulk) said : #2

You need to define global before the original definition.

Cheers,

Robert

Jan Stránský (honzik) said : #3

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

Robert Caulk (rcaulk) said : #4

Yes. I am assuming he's using a PyRunner - in which case he will need to call global in both places. If I am not mistaken. Throwing darts in the dark without an MWE tho.

Trishala DAKA (saidaka) said : #5

Yes Robert, I used PyRunner. I defined a list which is a collection of id in O.bodies [id] . I want to use that information inside a function. But I am not able to. So,I wrote a simple function to understand how it works.

Any way, I solved my problem.

Thanks.
Jan & Robert

Jan Stránský (honzik) said : #6

> Any way, I solved my problem.

for future reference, please tell us and other readers how you solved it.

cheers
Jan

Robert Caulk (rcaulk) said : #7

Classic ! :-)