Global and Local Variables RhinoPython

Hi, I’m trying to understand how global and local variable works INSIDE RHINOCEROS…
If I declare a variable outside (srfext and srfint) when I call it in Inverti function I got error (and crash of rhinoceros V5).
I tried to write global before but again I got error…

So, as global variable are foundamentals, can please someone explain me how to use it in Rhinoceros?

srfext = ""
srfint = ""

def SrfSel(self,sender,args):
    obj = rs.GetObject("Selezionare la lamiera")
    rs.ShrinkTrimmedSurface(obj)
    srf = rs.ExplodePolysurfaces(obj,False)
    srfextL = srf[0]
    srfintL = srf[1]
    srfext = srfextL
    srfint = srfintL
    rs.SelectObject(srfintL) 
    self._SurfTxt.Text = str(srfintL)

def Inverti(self,sender,args):
    print(srfint) 
    print(srfext)
    rs.UnselectAllObjects()
    if self._SurfTxt.Text == srfint:
       self._SurfTxt.Text == srfext
       rs.SelectObject(srfext)
    elif self._SurfTxt.Text == srfext:
       self._SurfTxt.Text == srfint
       rs.SelectObject(srfint)
    else:
       rs.MessageBox("Nessuna superficie trovata, ripetere la selezione")

P.S. Replace IronPython with Python please, is quite awful programming without full power of Python!

Are your functions class methods?
If so, you need to “declare” your global variables srfext and srfint as global inside the class or relevant class method, where you want to use them.

srfext = ""
srfint = ""

class Foo:
    def inverti(self, sender, args):
        global srfext, srfint
        print srfext, srfint

@diff-arch no is not a class… But declaring global srfext, srfint inside the function worked!
Thank you so much!

So, to sum up:

srfext = ""
srfint = ""

def SrfSel(self,sender,args):
    global srfext,srfint #THIS MUST BE WRITTEN IN ANY FUNCTION THAT USE GLOBAL VARIABLES
    srfext = "Example"
    srfint = "Example1"
   

def Inverti(self,sender,args):
    global srfext,srfint #THIS MUST BE WRITTEN IN ANY FUNCTION THAT USE GLOBAL VARIABLES
    print(srfint) 
    print(srfext)
    

What if you remove or rename the self parameter for each function?
It’s a default keyword reserved for class instances in Python and thus shouldn’t be used as symbol name otherwise! Maybe you using it, messes something up.

I’m using windows form so self is needed to access at form’s components like buttons, labels etcetc
But writing global inside the funcion worked well

OK, then your functions are probably class methods and you thus need to declare the global variables inside.

@diff-arch You are right! I’m inside the form class ='D Sorry