3-1-4-5. Variable Scope

Scope refers to which parts of a program a variable can be referenced or used from.

If a variable is created inside a function,

it can only be used within that function.

Consider these two functions,

word count and nearest square.

The first function uses answer to count words in a document.

The second function uses answer to check values while searching for the nearest square.

Both functions include this answer variable,

but they are distinct variables that can

only be referenced within their respective functions.

Alternatively, we might have a variable defined outside of functions, like this.

And we can access it anywhere outside or within these functions.

Scope is essential to understanding how information is passed throughout programs,

in Python, and really any programming language.

범위는 변수를 참조하거나 사용할 수 있는 프로그램 부분을 나타냅니다.

함수 내부에 변수가 생성되면

해당 함수 내에서만 사용할 수 있습니다.

이 두 가지 기능을 고려하면,

단어 수와 가장 가까운 제곱.

첫 번째 함수는 문서의 단어 수를 계산하기 위해 answer를 사용합니다.

두 번째 함수는 가장 가까운 사각형을 검색하는 동안 값을 확인하기 위해 답변을 사용합니다.

두 함수 모두 이 응답 변수를 포함합니다.

그러나 그것들은

각각의 기능 내에서만 참조됩니다.

또는 이와 같이 함수 외부에 정의된 변수가 있을 수 있습니다.

그리고 우리는 이러한 기능의 외부 또는 내부 어디에서나 액세스할 수 있습니다.

범위는 정보가 프로그램 전체에 전달되는 방식을 이해하는 데 필수적입니다.

Python과 실제로 모든 프로그래밍 언어에서.

Variable Scope

Variable scope refers to which parts of a program a variable can be referenced, or used, from.

It’s important to consider scope when using variables in functions. If a variable is created inside a function, it can only be used within that function. Accessing it outside that function is not possible.

# This will result in an error
def some_function():
    word = "hello"

print(word)

In the example above and the example below, word is said to have scope that is only local to each function. This means you can use the same name for different variables that are used in different functions.

# This works fine
def some_function():
    word = "hello"

def another_function():
    word = "goodbye"

Variables defined outside functions, as in the example below, can still be accessed within a function. Here, word is said to have a global scope.

# This works fine
word = "hello"

def some_function():
    print(word)

some_function()

Notice that we can still access the value of the global variable word within this function. However, the value of a global variable can not be modified inside the function. If you want to modify that variable’s value inside this function, it should be passed in as an argument. You’ll see more on this in the next quiz.

Scope is essential to understanding how information is passed throughout programs in Python and really any programming language.

More on Variable Scope

When you program, you’ll often find that similar ideas come up again and again. You’ll use variables for things like counting, iterating and accumulating values to return. In order to write readable code, you’ll find yourself wanting to use similar names for similar ideas. As soon as you put multiple piece of code together (for instance, multiple functions or function calls in a single script) you might find that you want to use the same name for two separate concepts.

Fortunately, you don’t need to come up with new names endlessly. Reusing names for objects is OK as long as you keep them in separate scope.

Good practice: It is best to define variables in the smallest scope they will be needed in. While functions can refer to variables defined in a larger scope, this is very rarely a good idea since you may not know what variables you have defined if your program has a lot of variables.

%d 블로거가 이것을 좋아합니다: