What's the analog of VB "Public SomeVariable1, SomeVariable2" in Python?

I need to keep the values of my variables for other scripts to use.

Python doesn’t have really a concept of hidden/private variables (other convention-based approach).

If you have a script somecode.py that has in the top scope a variable called some_variable1, then in othercode.py you can


some_variable1 = "Hello"


import somecode

Assuming you have proper directory structure and everything can be found by the interpreter.

In Rhino/Grasshopper, you also have the option to save variables (and other object types) in the sticky dictionary. Simply put, it exists above the user scripts of the current session, and thus each script of the session can read from it or save to it.


from scriptcontext import sticky

some_variable = "Hello World"
sticky["SomeVariable"] = some_variable # save to


from scriptcontext import sticky

some_variable = None # default

if "SomeVariable" in sticky.keys(): # exists
    some_variable = sticky["SomeVariable"] # fetch from

print some_variable

Imagine it kind of like a walky-talky or rather like how emailing works. some_script.py runs and puts essential information in the sticky dictionary. other_script.py fetches the information from the sticky dictionary, if it exists there.

1 Like

Thanks! In this case it makes sense to just use Document User Text. Values get saved with Rhino file too to be reused later.