Author Topic: We Need To Talk About Python - 1 Variables  (Read 5824 times)

0 Members and 3 Guests are viewing this topic.

claymic

  • $upporter
  • Sr. Member
  • *****
  • Posts: 152
  • Karma: 0
    • View Profile
We Need To Talk About Python - 1 Variables
« on: May 28, 2013, 09:16:19 AM »
OK, i decided to learn Python to use with Vox, i am more comfortable with Javascript and i really love it. Somethings in python let me crazy sometimes, so i decided to share here what i learn, when i get sometime free of course  ;D
Today i will try to explain how python treat Local and Global variables.
Dont worry about the previous explanations, the example will cover everything that you need.
- What is a Local Variable :
  Well, like the name suggest, "local variables" can only be used inside the function that was created.
- What is a Global Variables:
  You can call this variables from any place in the code.
Let see the examples and learn how we can change Global Variables inside a function.
Code: [Select]
#Global Variables
variable1 = "test1"
variable2 = "test2"
def changeVariables():
    variable1 = "change1"
    variable2 = "change2"
    #Local Variable
    variable3 = variable1+variable2
def changeVariablesGlobal():
    global variable1
    global variable2
    variable1 = "change1"
    variable2 = "change2"
Copy the code above and save in your "PY" folder, give the name TestVariables.py.
Now, see in the code that we have three variables. Two of them are declared outside the functions (variable1 and variable2), when we do that we are creating Global Variables and we can use this variables in any place of the code, see that i am using this variables in the function "changeVariables" to set the value of another variable.
The "variable3" otherwise its a Local Variable, was declared inside the function "changeVariables", so we can only use it inside this function, if you try to use this variable in the function "changeVariablesGlobal" you will get a error.
So far so good, but Python its a bad snake, we have to understand how to change the Global Variables now.
Lets put Vox to teach us. Copy the command bellow and paste in your VoxCommando, i think that you already now how to do that and i believe that you already put your script the folder PY, if you need change the path.
Code: [Select]
<?xml version="1.0" encoding="utf-16"?>
<command id="1791" name="variables" enabled="true" alwaysOn="False" confirm="False" requiredConfidence="0" loop="False" loopDelay="0" loopMax="0" description="">
  <action>
    <cmdType>PY.ExecFile</cmdType>
    <cmdString>PY\TestVariables.py</cmdString>
    <cmdRepeat>1</cmdRepeat>
  </action>
  <action>
    <cmdType>PY.ExecString</cmdType>
    <cmdString>changeVariables()</cmdString>
    <cmdRepeat>0</cmdRepeat>
  </action>
  <action>
    <cmdType>PY.ExecString</cmdType>
    <cmdString>changeVariablesGlobal()</cmdString>
    <cmdRepeat>1</cmdRepeat>
  </action>
  <action>
    <cmdType>PY.ExecString</cmdType>
    <cmdString>result = variable1 + " " + variable2</cmdString>
    <cmdRepeat>1</cmdRepeat>
  </action>
  <action>
    <cmdType>OSD.ShowText</cmdType>
    <cmdString>{LastResult}&amp;&amp;3000</cmdString>
    <cmdRepeat>1</cmdRepeat>
  </action>
</command>
So, now that you have the command we need to edit it to make some tests. Set your command like the picture bellow:

Ok, let understand the actions.
We will block the actions 2 and 3 (see the picture above) for this test.
Now, when we run the command, the action 1 will run the script file, everything that is outside a function will run, in our case we have only this outside the function:
Code: [Select]
variable1 = "test1"
variable2 = "test2"
So when Vox run the script this will set our global variables.
In the action 4 we will set our result to be equal to the variable1 and variable2, in the action 5 we show the result, so Vox will show this to you:

Nice...now we know how to set and use Global Variables in Vox.
To next test we will set the command like this:

Ok...see that now we enable the action 2. When we ask Vox to run the "PY.ExecString" we are saying to Vox to only run a small peace of code, in our case this means that we will run the function "changeVariables()", see:
Code: [Select]
def changeVariables():
    variable1 = "change1"
    variable2 = "change2"
    #Local Variable
    variable3 = "test3"
When we do that, the script will not run anything more, this means that the Global Variables will not be set again in our case.
What will be the result when we run the command in Vox ? Will be the same, the snake bite us ;D

In the function changeVariables() we are setting the values of the variable1 and the variable2, but when we do that we are not setting the value of the Global Variable, because Python will understand this like a Local Variable.
So, How we can change the value of a Global Variable ? Before  change the value of any Global Variable (if we are inside the function) we have to tell Python that it is a Global Variable. See the function changeVariablesGlobal()
Code: [Select]
def changeVariablesGlobal():
    global variable1
    global variable2
    variable1 = "change1"
    variable2 = "change2"
See ? We have to use the "global variable1", this will tell for Python that we will change the value of the Global Variable.
Now set your command in Vox like the picture bellow:

Run the command and now we will have a new result:

Ok...we can now use Global Variables in all our scripts, once we set the value of a Global Variable we can get this value from any place in our code, even in a different script.
Hope that help
Clayton
« Last Edit: May 28, 2013, 09:26:23 AM by claymic »

claymic

  • $upporter
  • Sr. Member
  • *****
  • Posts: 152
  • Karma: 0
    • View Profile
Re: We Need To Talk About Python - 1 Variables
« Reply #1 on: May 28, 2013, 09:41:51 AM »
Ok, i think that will be nice to explain how to get the value of the "variable3" that we set in the function "changeVariables()".
Lets change our script test and insert a new line on it:
Code: [Select]
#Global Variables
variable1 = "test1"
variable2 = "test2"
def changeVariables():
    variable1 = "change1"
    variable2 = "change2"
    #Local Variable
    variable3 = variable1+variable2
    return variable3
def changeVariablesGlobal():
    global variable1
    global variable2
    variable1 = "change1"
    variable2 = "change2"
We insert the "return variable3" in the function, when we do that Python will return any data of us, in this case it will return our variable3.
Now we have to insert a new action in VoxCommando

Get the files and make a test.
Hope that help
Clayton
« Last Edit: May 28, 2013, 10:13:59 AM by jitterjames »

Kalle

  • $upporter
  • Hero Member
  • *****
  • Posts: 2320
  • Karma: 47
    • View Profile
Re: We Need To Talk About Python - 1 Variables
« Reply #2 on: May 28, 2013, 09:42:44 AM »
Hi Clayton, nice tutorial - thanks for explain  :clap
***********  get excited and make things  **********

jitterjames

  • Administrator
  • Hero Member
  • *****
  • Posts: 7714
  • Karma: 116
    • View Profile
    • VoxCommando
Re: We Need To Talk About Python - 1 Variables
« Reply #3 on: May 28, 2013, 10:08:25 AM »
Thanks for sharing your experience on this.  Scope is a very important aspect of programming that you will encounter, and need to master, no matter what language you are using.  Although it is tempting to use global variables everywhere, you should avoid doing it whenever possible, because they make debugging much more difficult.

claymic

  • $upporter
  • Sr. Member
  • *****
  • Posts: 152
  • Karma: 0
    • View Profile
Re: We Need To Talk About Python - 1 Variables
« Reply #4 on: May 28, 2013, 10:17:17 AM »
Thanks Kalle.
James, sometimes there is no other way, we have to use Global Variables, but i will avoid to use this if possible. Thanks for the tip.
Clayton

jitterjames

  • Administrator
  • Hero Member
  • *****
  • Posts: 7714
  • Karma: 116
    • View Profile
    • VoxCommando
Re: We Need To Talk About Python - 1 Variables
« Reply #5 on: May 28, 2013, 10:22:56 AM »
Global variables are like "Vice-Grips"

 

My uncle used to say "Vice-Grips, are the wrong tool, for every job".  Meaning, it was easier to just use the vice-grips, because they were very versatile.  They always "fit" but they never "fit well", and usually they would cause damage to whatever they were being used on.

Global variables are like this.  They are easier to use, so why not use them all the time?  In fact, there is almost always another, better way to do it.

But I admit, I use them too, more than I should, but that is because I am a bad programmer.

claymic

  • $upporter
  • Sr. Member
  • *****
  • Posts: 152
  • Karma: 0
    • View Profile
Re: We Need To Talk About Python - 1 Variables
« Reply #6 on: May 28, 2013, 10:31:24 AM »
I am a bad programmer.
Really ?..... :bonk
If you are a bad programmer i dont know who will be good.
You are much modest today.

Kalle

  • $upporter
  • Hero Member
  • *****
  • Posts: 2320
  • Karma: 47
    • View Profile
Re: We Need To Talk About Python - 1 Variables
« Reply #7 on: May 28, 2013, 11:24:46 AM »
Global variables are like "Vice-Grips"

 

My uncle used to say "Vice-Grips, are the wrong tool, for every job".  Meaning, it was easier to just use the vice-grips, because they were very versatile.  They always "fit" but they never "fit well", and usually they would cause damage to whatever they were being used on.

Global variables are like this.  They are easier to use, so why not use them all the time?  In fact, there is almost always another, better way to do it.

But I admit, I use them too, more than I should, but that is because I am a bad programmer.
This is also a good tool  :biglaugh
My uncle says: "what does not fit is made ​​to fit"!

***********  get excited and make things  **********

claymic

  • $upporter
  • Sr. Member
  • *****
  • Posts: 152
  • Karma: 0
    • View Profile
Re: We Need To Talk About Python - 1 Variables
« Reply #8 on: May 28, 2013, 11:48:02 AM »
 :biglaugh :biglaugh :biglaugh :biglaugh :biglaugh :biglaugh
I have to remember to not ask you guys to fix anything for me in the future.

jitterjames

  • Administrator
  • Hero Member
  • *****
  • Posts: 7714
  • Karma: 116
    • View Profile
    • VoxCommando
Re: We Need To Talk About Python - 1 Variables
« Reply #9 on: May 28, 2013, 01:45:12 PM »
[ Invalid YouTube link ]

« Last Edit: September 14, 2014, 01:10:18 PM by jitterjames »

xtermin8r

  • $upporter
  • Sr. Member
  • *****
  • Posts: 366
  • Karma: 9
  • Crunchie
    • View Profile
Re: We Need To Talk About Python - 1 Variables
« Reply #10 on: May 28, 2013, 02:52:18 PM »
Neural Net Based Artificial Intelligence.

claymic

  • $upporter
  • Sr. Member
  • *****
  • Posts: 152
  • Karma: 0
    • View Profile
Re: We Need To Talk About Python - 1 Variables
« Reply #11 on: May 28, 2013, 03:41:24 PM »
 :biglaugh :biglaugh :biglaugh :biglaugh
James
When we use a Results.SetVar we are not setting a Global Variable too ? Do you think that we have to avoid it too ? I have to be very carefull now, i will insert several and several new function in Vox, i dot want to mess up with anything.
Thanks for your help
Clayton

jitterjames

  • Administrator
  • Hero Member
  • *****
  • Posts: 7714
  • Karma: 116
    • View Profile
    • VoxCommando
Re: We Need To Talk About Python - 1 Variables
« Reply #12 on: May 28, 2013, 05:57:22 PM »
Yes, sort of but in this case we need to do it.  It is not really global, but the scope is "user actions", which is what we need.  The same is true of {lastResult}.  This is basically a global variable but it is a convenient way to pass information from one action to the next.  It is not necessarily following a normal style of coding.

In particular, when you define a function, that function should accept input (parameters) and provide output (using return).  The function should not modify other variables "behind your back", it can get confusing.  In the case of setVar, the function is doing what is expected, it is setting this variable for us.  It is the same idea as storing a value in a database.

In any case, don't worry about using setVar if you need it.