BOOKPYTHON Telegram 3509
Когда в Python используется переменная, сначала она ищется в текущей области видимости. Если такая переменная не найдена, поиск продолжается во вложенной области. Это повторяется до тех пор, пока не будет достигнуто глобальное пространство имен.


x = 1
def scope():
x = 2
def inner_scope():
print(x) # выводит 2
inner_scope()
scope()


Однако присваивание переменной работает иначе. Новая переменная всегда создается в текущей области видимости, если не указано global или nonlocal:


x = 1
def scope():
x = 2
def inner_scope():
x = 3
print(x) # выводит 3
inner_scope()
print(x) # выводит 2
scope()
print(x) # выводит 1


global позволяет использовать переменные из глобального пространства имен, а nonlocal ищет переменную в ближайшей окружающей области видимости. Сравните:


x = 1
def scope():
x = 2
def inner_scope():
global x
x = 3
print(x) # выводит 3
inner_scope()
print(x) # выводит 2
scope()
print(x) # выводит 3



x = 1
def scope():
x = 2
def inner_scope():
nonlocal x
x = 3
print(x) # выводит 3
inner_scope()
print(x) # выводит 3
scope()
print(x) # выводит 1


👉@BookPython
👍13🔥21



tgoop.com/BookPython/3509
Create:
Last Update:

Когда в Python используется переменная, сначала она ищется в текущей области видимости. Если такая переменная не найдена, поиск продолжается во вложенной области. Это повторяется до тех пор, пока не будет достигнуто глобальное пространство имен.


x = 1
def scope():
x = 2
def inner_scope():
print(x) # выводит 2
inner_scope()
scope()


Однако присваивание переменной работает иначе. Новая переменная всегда создается в текущей области видимости, если не указано global или nonlocal:


x = 1
def scope():
x = 2
def inner_scope():
x = 3
print(x) # выводит 3
inner_scope()
print(x) # выводит 2
scope()
print(x) # выводит 1


global позволяет использовать переменные из глобального пространства имен, а nonlocal ищет переменную в ближайшей окружающей области видимости. Сравните:


x = 1
def scope():
x = 2
def inner_scope():
global x
x = 3
print(x) # выводит 3
inner_scope()
print(x) # выводит 2
scope()
print(x) # выводит 3



x = 1
def scope():
x = 2
def inner_scope():
nonlocal x
x = 3
print(x) # выводит 3
inner_scope()
print(x) # выводит 3
scope()
print(x) # выводит 1


👉@BookPython

BY Библиотека Python разработчика | Книги по питону


Share with your friend now:
tgoop.com/BookPython/3509

View MORE
Open in Telegram


Telegram News

Date: |

Telegram channels fall into two types: How to Create a Private or Public Channel on Telegram? Find your optimal posting schedule and stick to it. The peak posting times include 8 am, 6 pm, and 8 pm on social media. Try to publish serious stuff in the morning and leave less demanding content later in the day. Don’t publish new content at nighttime. Since not all users disable notifications for the night, you risk inadvertently disturbing them. The court said the defendant had also incited people to commit public nuisance, with messages calling on them to take part in rallies and demonstrations including at Hong Kong International Airport, to block roads and to paralyse the public transportation system. Various forms of protest promoted on the messaging platform included general strikes, lunchtime protests and silent sit-ins.
from us


Telegram Библиотека Python разработчика | Книги по питону
FROM American