Skip to content

Variables

Triple003 edited this page Jun 6, 2019 · 1 revision

Variables

To define a local variable, use this function

new_var(var_name, var_type, var_value)

To define a global variable, use this function

new_global(var_name, var_type, var_value)

To set a variable, use this function

set_var(var_name, var_value)

An example

from pythonOne import *

program = Program("py")
program.set_current_program()

new_global("a_global", int_type(), int_(22))

with start():
    new_var("a_local", str_type(), str_("sdfqwezxc"))
    printit("a_local")
    set_var("a_local", str_("Changed"))
    printit("a_local")

program.build()

"""
Prints:
sdfqwezxc
Changed
"""

Let's break this down.
First, we import pythonOne and create a program:

from pythonOne import *

program = Program("py")
program.set_current_program()

Next, we create a global variable

new_global("a_global", int_type(), int_(22))

A global variable is a variable that can be accessed by all functions, the start context manager, and event handlers.
Then first argument is the name of the variable, the second is the type of the variable, and the third is the value.
The int_type() function tells pythonOne that the value that the variable is holding an integer.
The int_(value) functions tells pythonOne that value is an integer.
After this we define the start:

with start():

Then we define a local variable and printit

new_var("a_local", str_type(), str_("sdfqwezxc"))
printit("a_local")

A local variable behaves like a global variable, except it can be only accessed in the function, start context manager, or event handler it is in.
Next, we change the variable and printit

set_var("a_local", str_("Changed"))
printit("a_local")

When we set a variable, we must first make sure that we have already defined the variable.
Finally we build the programL

program.build()

Clone this wiki locally