local vs global python

I revisited this concept after reading through some forum posts.
I wanted to add something to it, but decided to write up some code for visual people like me. 

It's so much easier to see it in action, then have someone explain it to me.
So here is some code, try it out, see how the local and global stuff reacts.

Hope to have some more bigger projects soon!

####### local_global.py   ##########

a_list = []
b = []
adictionary = {}
astring = "hello world"
an_integer = 8
a_boolean = True

def print_globals():
    global astring
    global an_integer
    global a_boolean
    print("=" * 10)
    print('b = ', b)
    print('a_list =', a_list)
    print('adictionary = ', adictionary)
    print('astring = ', astring)
    print('an_integer = ', an_integer)
    print('a_boolean = ', a_boolean)
    print("=" * 10)


def change_globals():
    b.append('y')
    # if another local variable is created with the same
    # name as a mutable type global variable,
    # the global will not be accessible

             ##--------##
    #a_list.append('nani?') <--- this will not work
             ##--------##
    adictionary['howdy'] = 3
    #######     the string    #######
    try:
        astring += 'nice to meet ya.'
    except:
        message = '(astring):global variables can not have their identity changed without permission.\n'
        print(message)
     ######### the integer ########
    try:
        an_integer += 2
    except:
        message = '(an_integer): you can not change this without specific language for good reason.\n'
        print(message)
    #######   the boolean  ########
    try:
        a_boolean = False
    except:
        message = 'What if your global (a_boolean) was not the variable you meant to change on a global scope? \n'
        print(message)

    #########   the list  #########
    try:
        a_list.append('huh?')
    except:
        message = "why does the variable b work, but not alist?\n"
        print(message)
   #######   the local variable  ##########
    try:
        a_list = True
        message = "if this was a global change, my global list, a_list would be lost."
        message2 = "\n     local a_list = "
        print(message, message2, a_list, "\n")
    except:
        message= """if the a_list variable is created,
                    locally with assignment, I can now not alter the global
                    without declaring that is the variable I am accessing.\n"""

        print(message)

    ########   the global variable without declaration  ##########
    try:
        # Now a_list has a local value, and I can not access the global without declaring
        # that it is the variable I want to access.
        a_list.append("This will not work now.")
    except:
        message = "now try to access the global. \n"
        print(message)
    #######   trying to access the global a_list now  ##########
    try:
        # Uncomment the < global a_list > to see try block fail
        #global a_list  #<--- it won't even let me use it in a try block

        print(a_list)
    except:
        message = """since we made a local a_list variable, the global can not
                  be used in tandem with the local variable.  This would be bad."""

        print(message)


print_globals()
change_globals()
print_globals()

Comments

Popular posts from this blog

playing with color in powershell python

JavaScript Ascii animation with while loops and console.log

playing with trigonometry sin in pygame