Local/global scope

drwie

I failed this question on a test and I was wondering if someone could help me understand why. The question: Which names occur in the local scope?

a = 3
b = 6

def f(a):
    c = a + b
    return c

I answered a,b,c, but apparently it's just a and c. Why is b not 'occurring' in the local scope?

Tom Karzes

There are global instances of a and b, due to their assignments at the file level.

Within function f, there is a parameter a which makes that instance of a local (and independent from the global instance of a). b is referenced within f, but is not assigned to, so b refers to the global instance of b. c is assigned to within f, so c is local.

So f has local instances of a and c.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related