Python equivalent for VBS Private to store variables for a session


In RS I used the Private variable a lot outside a sub to store values for the duration of a session.
How would I go about doing this in Python.

Private oldRadius
If IsEmpty(oldRadius) Then oldRadius = 1


Hi Willem,

you can put data into any module. Or (with that idea), you can also put data into the scriptcontext.sticky dictionary. That’s also data in a module.


Giulio Piacentino
for Robert McNeel & Associates

Hi Giulio,

Thanks for the answer.
I tried to get this to work and came up with this, would you say this is correct and pythonesque?

import scriptcontext as sc
import rhinoscriptsyntax as rs

def storevariable():
    #check for absence of stored previous/old key-value pair
    if "oldradius" not in sc.sticky:
        #set default value
    # ask user input for real number set the default to oldradius from sticky
    real = rs.GetReal("set real",sc.sticky["oldradius"])
    # return when probably cancelled out
    if (real == None) : return
    # set new value to key in sticky
    print sc.sticky.get("oldradius")



I just found I mistakenly asked for a storage between sessions where in fact I want it stored for the duration of the session.

Hi Willem,

There is actually an “official” example from Steve somewhere, but i don’t remember where it’s located - I copied it out for reference a long time ago, see below:

# The scriptcontext module contains a standard python dictionary called
# sticky which "sticks" around during the running of Rhino. This dictionary
# can be used to save settings between execution of your scripts and then
# get at those saved settings the next time you run your script -OR- from
# a completely different script.
import rhinoscriptsyntax as rs
import scriptcontext

stickyval = 0
# restore stickyval if it has been saved
if scriptcontext.sticky.has_key("my_key"):
    stickyval = scriptcontext.sticky["my_key"]
nonstickyval = 12

print "sticky =", stickyval
print "nonsticky =", nonstickyval

val = rs.GetInteger("give me an integer")
if val:
    stickyval = val
    nonstickyval = val

# save the value for use in the future
scriptcontext.sticky["my_key"] = stickyval

My own “shorthand” version:

import scriptcontext as sc

if sc.sticky.has_key("key"): value = sc.sticky["key"]
else: value = defaultValue

sc.sticky["key"] = newValue


Hi Mitch,

Thanks for that confirmation, I enjoy that my solution is so similar to Steve’s example.

FYI: I just found that " has_key() is deprecated in favor of key in d "


found your possible source: