Telegram Group & Telegram Channel
Когда в 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



tg-me.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 разработчика | Книги по питону


Warning: Undefined variable $i in /var/www/tg-me/post.php on line 283

Share with your friend now:
tg-me.com/BookPython/3509

View MORE
Open in Telegram


Библиотека Python разработчика Telegram | DID YOU KNOW?

Date: |

How Does Bitcoin Work?

Bitcoin is built on a distributed digital record called a blockchain. As the name implies, blockchain is a linked body of data, made up of units called blocks that contain information about each and every transaction, including date and time, total value, buyer and seller, and a unique identifying code for each exchange. Entries are strung together in chronological order, creating a digital chain of blocks. “Once a block is added to the blockchain, it becomes accessible to anyone who wishes to view it, acting as a public ledger of cryptocurrency transactions,” says Stacey Harris, consultant for Pelicoin, a network of cryptocurrency ATMs. Blockchain is decentralized, which means it’s not controlled by any one organization. “It’s like a Google Doc that anyone can work on,” says Buchi Okoro, CEO and co-founder of African cryptocurrency exchange Quidax. “Nobody owns it, but anyone who has a link can contribute to it. And as different people update it, your copy also gets updated.”

Библиотека Python разработчика from us


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