Friday 25 May 2018

function - Incrementing integer variable of global scope in Python

I am trying to change global value x from within another functions scope as the following code shows,



x = 1
def add_one(x):

x += 1


then I execute the sequence of statements on Python's interactive terminal as follows.



>>> x
1
>>> x += 1
>>> x
2

>>> add_one(x)
>>> x
2


Why is x still 2 and not 3?

No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...