A little question about python variable scope

I am a beginner python and am wondering very confusing for me. If I define a function first, but inside a function, I have to use a variable that is defined in another function below, can I do it like this? Or how can I import the returned things of another function into a function? eg:

def hello(x,y):
    good=hi(iy,ix)
    "then do somethings,and use the parameter'good'."
    return something

def hi(iy,ix):
    "code"
    return good

      

+1


source to share


5 answers


The scope of functions hello

and is hi

completely different. They don't have any variables.

Note that the call results in hi(x,y)

some object. You store this named object good

in a function hello

.

The variable named good

in hello

is another variable that is not associated with the variable named good

in the function hi

.

They are spelled the same, but they exist in different namespaces. To prove this, change the spelling of the variable good

in one of the two functions, you can see that it still works.


Edit. Follow-up observation: "so what should I do if I want to use the result of a function hi

in a function hello

?"



Nothing unusual. Take a look hello

.

def hello(x,y):
    fordf150 = hi(y,x)
    "then do somethings,and use the variable 'fordf150'."
    return something

def hi( ix, iy ):
    "compute some value, good."
    return good

      

Some script is evaluating hello( 2, 3)

.

  • Python creates a new namespace for evaluation hello

    .

  • The hello

    object is x

    attached to the object 2

    . The linking is done in order.

  • The hello

    object is y

    attached to the object 3

    .

  • The hello

    Python evaluates the first operator fordf150 = hi( y, x )

    , y

    is 3, x

    is equal to 2.

    and. Python creates a new namespace for evaluation hi

    .

    b. The hi

    object is ix

    attached to the object 3

    . The linking is done in order.

    from. The hi

    object is iy

    attached to the object 2

    .

    In hi

    something happens and is good

    attached to some object, say 3.1415926

    .

    e. B hi

    holds a return

    ; identifying the object as a value for hi

    . In this case, the object is called good

    and is an object 3.1415926

    .

    f. The namespace is hi

    discarded. good

    , ix

    and iy

    disappear. However, the object ( 3.1415926

    ) remains as a score value hi

    .

  • In hello

    , Python completes the first statement fordf150 = hi( y, x )

    , y

    is 3, x

    is equal to 2. The value of hi

    the same 3.1415926

    .

    and. fordf150

    attached to the object created by calculating hi

    , 3.1415926

    .

  • In hello

    Python, it goes to other statements.

  • At some point something

    attached to an object, say 2.718281828459045

    .

  • B hello

    is executed a return

    ; identifying the object as a value for hello

    . In this case, the object is called something

    and is an object 2.718281828459045

    .

  • The namespace is discarded. fordf150

    and something

    disappear, like x

    and y

    . However, the object ( 2.718281828459045

    ) remains as a score value hello

    .

Any program or script called hello

gets a response.

+5


source


If you want to define a variable in the global namespace from within a function and thereby make it available to other functions in that function, you can use the global keyword. Here are some examples

varA = 5   #A normal declaration of an integer in the main "global" namespace

def funcA():
    print varA #This works, because the variable was defined in the global namespace
               #and functions have read access to this.
def changeA():
    varA = 2  #This however, defines a variable in the function own namespace
              #Because of this, it not accessible by other functions.
              #It has also replaced the global variable, though only inside this function
def newVar():
    global varB #By using the global keyword, you assign this variable to the global namespace
    varB = 5

def funcB():
    print varB #Making it accessible to other functions

      

Conclusion: Variables defined in a function remain in the function's namespace. It still has read-only access to the global namespace, unless the variable was called with the global keyword.



The term global is not completely global as it might seem at first glance. It is practically just a reference to the lowest namespace in the file you are working in. Global keywords are not available in another module.

As a mild warning, this may be considered less "good practice" for some.

+3


source


your example program works because the two instances of "good" are different variables (you just have both variables with the same name). The following code is exactly the same:

def hello(x,y):
    good=hi(iy,ix)
    "then do somethings,and use the parameter'good'."
    return something

def hi(iy,ix):
    "code"
    return great

      

+2


source


More details on python definition rules are given here:

Summary of Python Review Rules

+2


source


The "hi" function doesn't mind if you call the "hi" function that is not yet defined, unless you actually try to use the "hi" function until both functions are defined.

+1


source







All Articles