Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 23305

Why is Python built like this? [closed]

$
0
0

I understand this code:

test_variable = 0def test_function():    test_variable = 1test_function()print(test_variable)

Because the test_function does not declare a global test_variable, the actual global test_variable is not changed, so the output is 0.

However, it seems to work differently if I use a dictionary:

resources = {"water": 300,"milk": 200,"coffee": 100,}def test_function():    resources['water'] = 500test_function()print(resources)

Now I get the result: {'water': 500, 'milk': 200, 'coffee': 100}.

Why did the value for 'water' inside the resources dictionary change, even though the new test_function still does not use global?


Viewing all articles
Browse latest Browse all 23305

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>