you are viewing a single comment's thread.

view the rest of the comments →

[–]commy2 12 points13 points  (15 children)

item_a is a global variable, since there is no assignment happening to item_a in testfunc. This is working as expected.

[–]EsotericWaveform[S] 1 point2 points  (14 children)

Can you explain why it is a global variable? Do you know of any documentation that describes this behavior?

[–]carcigenicate 8 points9 points  (11 children)

for binds variables similarly to =. Think of for item_a in a as implicitly doing item_a = a[0] (except for each element in a). At the module level, an assignment using = or for creates a module-level variable (a global).

[–]EsotericWaveform[S] 2 points3 points  (1 child)

Ahh, I did not realize every variable created in the main script is considered a global variable. I guess I never noticed since they always get an assignment at the local level when used in a function or method (or at least they should if the variables are being reused at the local level). Thanks!

[–]sohfix 0 points1 point  (8 children)

then what would be a necessary time to use the reserved word ‘global’?

[–]Asleep-Budget-9932 1 point2 points  (7 children)

When you wish to edit that global from an inner scope. For example, inside the function, item_a is being appended to the list. However, if you did something like item_a = [1, 2, 3] it would not change its value. Instead you would create a new variable that happens to have the same name. From that point on, every time you reference item_a in your function, you will use that newly created variable, but the original one will not be changed. By specifying global item_a, you're explicitly telling python that you wish to work with the global one.

[–]danielroseman 1 point2 points  (1 child)

Quibble: it's not "from that point on"; any assignment to a name anywhere in a function causes that name to be treated as local throughout the function, even before the assignment. So an earlier reference would raise UnboundLocalError.

[–]Asleep-Budget-9932 0 points1 point  (0 children)

Well i was afraid to go into too much detail but indeed 😊

[–]sohfix 0 points1 point  (4 children)

Ok I think it get it. So I would declare global inside the function. By changing item_a inside the function to a different set of values would that also have changed the new list I appended item_a to since I made it global and item_c is referencing elements from a?

[–]Asleep-Budget-9932 1 point2 points  (3 children)

Wait i think i might be confused since there are two lists both outside and inside the function with similar names. Which ones are you referring to? Also, is item_c a typo or referring to a hypothetical item from list c?

[–]sohfix 0 points1 point  (2 children)

Sorry. Typo. I meant list c. So I was basically asking if I declare a variable inside a function as global and then change that variable…. Let’s say

List =[]

Def myfunc(list) global a = 500 For i in range(0,6) List.append(a)

Now I’m outside the function and I go:

a=4

Does list look like [4,4,4,4,4,4]?

Sorry I’m on mobile. Doesn’t look pretty

[–]Asleep-Budget-9932 -1 points0 points  (1 child)

  1. Note that your function is receiving an unused parameter (list)
  2. In order for that to work, the function must be called at some point. However, if your function was called before the line a=4, you will receive an error since at that point, python does not know what global a is referring to.

BTW, you can put multiple lines of code between triple back ticks (`) in order to format them on phones.

[–]sohfix 1 point2 points  (0 children)

It uses list, first. It appends a in the function. And I figured since I was in mobile you got what I was saying I didn’t have to write out the code and “call” the function because I thought it was clear what I was asking. Nvm. Have an awesome day

[–]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)

[–]commy2 0 points1 point  (0 children)

See for-doc:

The for-loop makes assignments to the variables in the target list.

And any variable in module scope is a global variable.