About functions and their outside data access | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

About functions and their outside data access

From a function in python, you have global read-only access, but you can only overwrite (non-globalized) data with certain container methods (which is still a lot of power that comes with responsibility etc pp). Some things surprise me though; if it's not just implementation decisions but there's a logic behind it, I'd be happy to hear an explanation. 1.) You have reading access, so you can read list 'L' from a function. You can also write L = [1, 2], then the global L is ignored and the function will have its own L. You can't write L=L though; I thought that it would read the outside L into the inside version, but it gives an error... 2.) You can modify containers via methods. L.append(stuff) or L.extend(stuff) will work. You can not use L += stuff, although it's often taught as 'synonymous' with L.extend(stuff). 3.) Yeah, that's because you can't assign, might be the answer. But you can indeed write L[5]=stuff. Why is this different?

21st Oct 2018, 6:05 PM
HonFu
HonFu - avatar
8 Answers
+ 5
Short Answer:  If there is an assignment to a variable inside a function, that variable is considered local. https://stackoverflow.com/q/9264763/9132046
21st Oct 2018, 6:50 PM
Mert Yazıcı
Mert Yazıcı - avatar
+ 5
They look similar but their implementations are different https://hg.python.org/cpython/file/db842f730432/Objects/listobject.c#l914 https://stackoverflow.com/q/23564955/9132046 and l[1] = 2 is probably valid because it is same as l.__setitem__(1, 2)
21st Oct 2018, 7:48 PM
Mert Yazıcı
Mert Yazıcı - avatar
+ 3
The id change and don't change because . L = L+[something] not equals += [something] From python documentation " x += 1 can be rewritten as x = x + 1 to achieve a similar, but not exactly equal effect. In the augmented version, x is only evaluated once. Also, when possible, the actual operation is performed in-place, meaning that rather than creating a new object and assigning that to the target, the old object is modified instead. " https://docs.python.org/3/reference/simple_stmts.html#grammar-token-target-list append is different from extend too list.append(x) Equivalent to a[len(a):] = [x]. list.extend(iterable). Equivalent to a[len(a):] = iterable. https://docs.python.org/3/tutorial/datastructures.html Yeah it seems that assignment have a lot of different rules For example : In list we can have x[1:2] = x[1:2] = 3 + 11, 22 The right part and yours expressions are avaliated/calculated first. After this the expression inside [] is avaliated. If everithyng is on the right spot the object calculated in right part can be assigned where left part "says" to assign Here we can see all assignment rules. And see that assignments have different classifications and funcionality. https://docs.python.org/3/reference/simple_stmts.html#grammar-token-target-list
21st Oct 2018, 8:09 PM
Anya
Anya - avatar
+ 2
For the 1) if i understand right this will be helpfull https://www.sololearn.com/discuss/1553059/?ref=app
21st Oct 2018, 6:10 PM
Anya
Anya - avatar
+ 2
HonFu .Oh sorry. So really good questioning . i will check compiled material that i wrote. I think i still have here. it s about Python study case. Probably have something related.
21st Oct 2018, 6:24 PM
Anya
Anya - avatar
+ 2
Thanks, Mert, it was an interesting read. But it couldn't fully answer my question(s). So as soon as Python reads L= in a function, L is 'declared' as local. After that, if I continue with L, there will not be that L, since the door to the outside L is shut now. So far so good, but += in relation to lists is an operator overload, right? For example when I write L = L+[something], the id of the object changes; but when I write L += [something], the id doesn't change, just like with extend or append. It seems to be an overload basically calling extend, but still is called out as an assignment. And if Python is so strict about assignments shutting the door to the outside world after it reads =, how can L[1] =... suddenly be okay? As long as I/we don't make sense of all of this, there will again and again be strange, basic errors.
21st Oct 2018, 7:11 PM
HonFu
HonFu - avatar
+ 2
Thank you, Mert, and Anya, for the links, that's quite interesting (will need a bit of time to digest that though ^^). So even if += in relation to lists is an operator overload, it still ends up as an assignment with __iadd__, while via __setitem__ it's different... I find interesting the difference between x = x+1 and x += 1. I thought it is the same, but the order of operation is different and also the object identity. There's so much hidden under Python's deceiving simplicity ...
21st Oct 2018, 9:53 PM
HonFu
HonFu - avatar
+ 1
Anya, I know about global; what I am trying is understand the logic behind why certain things work while others don't, although they look similar. So the three questions are connected and form a single question.
21st Oct 2018, 6:17 PM
HonFu
HonFu - avatar