Python Scope – Python Хамрах хүрээ #

A variable is only available from inside the region it is created. This is called scope.

Python-д хувьсагчийг үүссэн газарт нь ашиглагддаг бөгөөд үүнийг хамрах хүрээ гэж нэрлэдэг.

Local Scope – Локал Хамрах Хүрээ #

A variable created inside a function belongs to the local scope of that function, and can only be used inside that function.

Функцийн дотор үүссэн хувьсагч нь тухайн функцийн локал хамрах хүрээнд хамаардаг бөгөөд зөвхөн тэр функцийн дотор ашиглагдана.

Example – Жишээ #

A variable created inside a function is available inside that function:

Функцийн дотор үүсгэсэн хувьсагч нь тухайн функц дотор байдаг:

def myfunc():
  x = 300
  print(x)
myfunc()

Function Inside Function – Функцийн Доторх Функц #

As explained in the example above, the variable x is not available outside the function, but it is available for any function inside the function:

Дээрх жишээнд x хувьсагч функцийн гаднаас ашиглах боломжгүй боловч, тухайн функцийн доторх өөр функцэд ашиглах боломжтой:

Example – Жишээ #

The local variable can be accessed from a function within the function:

Локал хувьсагчийг функц доторх функцээс хандалт хийж болно:

def myfunc():
  x = 300
  def myinnerfunc():
    print(x)
  myinnerfunc()
myfunc()

Global Scope – Глобал Хамрах Хүрээ #

A variable created in the main body of the Python code is a global variable and belongs to the global scope.

Python кодын үндсэн хэсэгт үүсгэсэн хувьсагчийг глобал хувьсагч гэж нэрлэгддэг бөгөөд глобал хамрах хүрээнд хамаарна. Глобал хувьсагч нь бүх хамрах хүрээн дотор, тэр дундаа глобал болон локал хамрах хүрээнд ашиглагдах боломжтой.

Global variables are available from within any scope, global and local.

Глобал хувьсагч нь функцийн гадна болон дотор ашиглагдаж, өөр функцээс хандах боломжтой байдаг.

Example – Жишээ #

A variable created outside of a function is global and can be used by anyone:

Функцийн гадна үүсгэсэн хувьсагч нь глобал хувьсагч бөгөөд хаана ч ашиглаж болно.

x = 300
def myfunc():
  print(x)
myfunc()
print(x)

Naming Variables – Хувьсагч Нэрлэх #

If you operate with the same variable name inside and outside of a function, Python will treat them as two separate variables, one available in the global scope (outside the function) and one available in the local scope (inside the function):

Хэрэв функцийн дотор болон гадна ижил нэртэй хувьсагч ашиглавал, Python нь тэдгээрийг тусдаа хувьсагч гэж үзнэ. Функцийн гаднах хувьсагч нь глобал хамрах хүрээнд, харин доторх нь локал хамрах хүрээнд хамаарна.

Example – Жишээ #

The function will print the local x, and then the code will print the global x:

 

x = 300
def myfunc():
  x = 200
  print(x)
myfunc()
print(x)

Global Keyword – Глобал Түлхүүр Үг #

If you need to create a global variable, but are stuck in the local scope, you can use the global keyword.

Хэрэв та локал хамрах хүрээнд байгаа үед глобал хувьсагч үүсгэх шаардлагатай бол global түлхүүр үгийг ашиглаж болно. 

The global keyword makes the variable global.

global түлхүүр үг нь тухайн хувьсагчийг глобал болгоно.

Example – Жишээ #

If you use the global keyword, the variable belongs to the global scope:

Хэрэв global түлхүүр үгийг ашиглавал, хувьсагч глобал хүрээнд хамаарагддаг:

def myfunc():
  global x
  x = 300
myfunc()
print(x)

Also, use the global keyword if you want to make a change to a global variable inside a function.

Түүнчлэн, global түлхүүр үгийг ашиглан функцийн дотор глобал хувьсагчийн утгыг өөрчлөх боломжтой.

Example – Жишээ #

To change the value of a global variable inside a function, refer to the variable by using the global keyword:

Функцийн дотор глобал хувьсагчийн утгыг өөрчлөхийн тулд, тухайн хувьсагчийг global түлхүүр үгээр зааж өгнө:

x = 300
def myfunc():
  global x
  x = 200
myfunc()
print(x)

Nonlocal Keyword – Nonlocal түлхүүр үг #

The nonlocal keyword is used to work with variables inside nested functions.

nonlocal түлхүүр үг нь бие биедээ орсон функцүүд доторх хувьсагчидтайгаа ажиллахад ашиглагддаг.

The nonlocal keyword makes the variable belong to the outer function.

nonlocal түлхүүр үг нь хувьсагчийн гадна функцэд харьяалуулдаг.

Example #

If you use the nonlocal keyword, the variable will belong to the outer function:

def myfunc1():
  x = "Jane"
  def myfunc2():
    nonlocal x
    x = "hello"
  myfunc2()
  return x
print(myfunc1())

Powered by BetterDocs

Leave a Reply