← Back to team overview

yade-users team mailing list archive

Re: [Question #239546]: Functions "not defined" through yade terminal, but defined through script ?

 

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

    Status: Open => Answered

Bruno Chareyre proposed the following answer:
Welcome to python scopes [1]!
Symbols defined in the script are global, defined in terminal they are
local (I would love if running a script or typing it in the terminal was
exactly the same, but it is not...)

Put this in a script: "def printItScript(): print O.iter" and start yade
with this script.
Now in the terminal type "def printItTerminal(): print O.iter"

Check where they are:
Yade [1]: globals().has_key('printItScript')
 ->  [1]: True

Yade [2]: locals().has_key('printItScript')
 ->  [2]: False

Yade [1]: globals().has_key('printItTerminal')
 ->  [1]: False

Yade [2]: locals().has_key('printItTerminal')
 ->  [2]: True

Now guess where the PyRunner's command is looked for?...

A solution is this:
 
def printIt(): print O.iter
globals()['printIt']=locals()['printIt']  #anyone knows a better way?
O.engines=[PyRunner(command='printIt()',iterPeriod=1)]
O.run(1)

Bruno

[1]
https://www.inkling.com/read/learning-python-mark-lutz-4th/chapter-17/python-scope-basics

-- 
You received this question notification because you are a member of
yade-users, which is an answer contact for Yade.