yade-users team mailing list archive
-
yade-users team
-
Mailing list archive
-
Message #25036
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
Status: Open => Answered
Jan Stránský proposed the following answer:
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
--
You received this question notification because your team yade-users is
an answer contact for Yade.