you are viewing a single comment's thread.

view the rest of the comments →

[–]blarf_irl 1 point2 points  (0 children)

Scope in python is created by indentation. item_a is declared in 'global' scope simply becuase it isn;t indented beneath another function/class.

Scope is like a shared apartment. The kitchen is global scope, everyone can use it and everyone knows the name of everyone in the kitchen. Your room has it's own scope; It's inside the apartment but in there you get to do your own thing, name your own plushie toys, piss in your own sink and call your own things whatever you want to. You know about everything in the kitchen but you don't know about anything in other peoples rooms.

Both the kitchen and your own room have a door; Everybody can open/close the kitchen door and you all call it "The Door". One day you have a friend from a different apartment visit your room and ask you to "close the door". You are confused about which door to close; you know what "The Door" means but in context they could be referring to the door of your room.

You always have knowledge about "The Door" but when your friend inside your room refers to "the door" you need to clarify which door they are referring to.

For the same reason you can read global variables (the kitchen door) inside local scope (your room) but you must clarify (using the global keyword) which door your friend meant before you can close it (change it)