Why does the scope work like this?

User

This question is a result of a student of mine asking a question about the following code, and I'm honestly completely stumped. Any help would be appreciated.

When I run this code:

#test 2

a = 1

def func2(x):
    x = x + a
    return(x)

print(func2(3))

it works perfectly fine. It is able to take the globally-scoped variable a and use its value to perform the calculation and return the value 4.

However, if I change it to this:

# test 3

a = 1

def func3(x):
    a = x + a
    return(x)

print(func3(3))

I then get an error:

local variable 'a' referenced before assignment

Why do I get this error only when I want to update the value of a within the function to a new value based on its original value? What am I not understanding? I feel like this second piece of code should work fine.

Thanks in advance for any help and insight.

shiva
a = 1

def func3(x):
    global a
    a = x + a
    return(x)

print(func3(3))

Now it should work.

When you put the statement a=x+a inside the function, it creates a new local variable a and tries to reference its value(which clearly hasnt been defined before). Thus you have to use global a before altering the value of a global variable so that it knows which value to refer to.

EDIT:

The execution of a function introduces a new symbol table used for the local variables of the function. More precisely, all variable assignments in a function store the value in the local symbol table; whereas variable references first look in the local symbol table, then in the local symbol tables of enclosing functions, then in the global symbol table, and finally in the table of built-in names. Thus, global variables cannot be directly assigned a value within a function (unless named in a global statement), although they may be referenced.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Why does array.sort not work by an array like this?

From Dev

Why does ng-click="test()" not work but onclick="angular.element(this).scope().test()" does?

From Dev

why does this .pop(0) work like this

From Dev

How does scope work in Io?

From Dev

mysql not like does not work

From Dev

Using a local variable outside of its declaring scope; why does this work?

From Dev

Angular $scope does not work

From Dev

Why does map work like izip_longest with fill=None?

From Dev

Why does printing a variable in global scope work, but modifying it does not?

From Dev

Why does the java substring method work like this?

From Dev

Why does sorting the array like this not work?

From Dev

Why bitwise division does not work like bitwise multiplication?

From Dev

$scope not injected, still referenced. Why does it work?

From Dev

Why does array.sort not work by an array like this?

From Dev

Why bitwise division does not work like bitwise multiplication?

From Dev

why set-car! implement like this does not work

From Dev

Why does "xinput list" work just like "xinput --list"?

From Dev

mysql not like does not work

From Dev

How does scope work in Ruby?

From Dev

Why does facebook not like this

From Dev

Why does $scope.$watch work but $scope.$watchCollection does not?

From Dev

Angular $scope does not work

From Dev

Why Does `'ln -s /directory 'D:'` Work Like It Does?

From Dev

Why does an alias work sometimes like a nameref and sometimes not?

From Dev

Why does printing a variable in global scope work, but modifying it does not?

From Dev

Why does it work with $scope but not with `this`?

From Dev

Why does creating an instance of a generic type work like this?

From Dev

Why does static IP in Ubuntu 16 work only like this?

From Dev

Why does my rest-like routing not work?

Related Related

  1. 1

    Why does array.sort not work by an array like this?

  2. 2

    Why does ng-click="test()" not work but onclick="angular.element(this).scope().test()" does?

  3. 3

    why does this .pop(0) work like this

  4. 4

    How does scope work in Io?

  5. 5

    mysql not like does not work

  6. 6

    Using a local variable outside of its declaring scope; why does this work?

  7. 7

    Angular $scope does not work

  8. 8

    Why does map work like izip_longest with fill=None?

  9. 9

    Why does printing a variable in global scope work, but modifying it does not?

  10. 10

    Why does the java substring method work like this?

  11. 11

    Why does sorting the array like this not work?

  12. 12

    Why bitwise division does not work like bitwise multiplication?

  13. 13

    $scope not injected, still referenced. Why does it work?

  14. 14

    Why does array.sort not work by an array like this?

  15. 15

    Why bitwise division does not work like bitwise multiplication?

  16. 16

    why set-car! implement like this does not work

  17. 17

    Why does "xinput list" work just like "xinput --list"?

  18. 18

    mysql not like does not work

  19. 19

    How does scope work in Ruby?

  20. 20

    Why does facebook not like this

  21. 21

    Why does $scope.$watch work but $scope.$watchCollection does not?

  22. 22

    Angular $scope does not work

  23. 23

    Why Does `'ln -s /directory 'D:'` Work Like It Does?

  24. 24

    Why does an alias work sometimes like a nameref and sometimes not?

  25. 25

    Why does printing a variable in global scope work, but modifying it does not?

  26. 26

    Why does it work with $scope but not with `this`?

  27. 27

    Why does creating an instance of a generic type work like this?

  28. 28

    Why does static IP in Ubuntu 16 work only like this?

  29. 29

    Why does my rest-like routing not work?

HotTag

Archive