為什麼帶註釋的變量不能是全局的? (Why can't an annotated variable be global?)


問題描述

為什麼帶註釋的變量不能是全局的? (Why can't an annotated variable be global?)

跑步

global global_values
global_values: str = []

SyntaxError: annotated name 'global_values' can't be global

這有什麼原因嗎?


參考解法

方法 1:

This has been explained in PEP‑526:

It is illegal to attempt to annotate variables subject to global or nonlocal in the same function scope:

def f():
    global x: int  # SyntaxError

def g():
    x: int  # Also a SyntaxError
    global x

The reason is that global and nonlocal don't own variables; therefore, the type annotations belong in the scope owning the variable.

方法 2:

So, as is often the case with very old language features, this is confusing. global in python is a statement that declares a name to be matching a key in the globals() dictionary‑like container. If the key is not already in the globals() dictionary‑like container, it will be added. It will default to a value of None at that time. If a variable is assigned in the global scope (and defined at that time), then the variable's name is added to the globals() dictionary‑like container and the value is set to the value for the key matching the name in the globals() dictionary‑like container.

So, the global keyword is a statement that adds a name to that globals() dictionary‑like container. Unfortunately, you cannot do assignments to the variable in the same line as using global to add that variable's name to globals() dictionary‑like container.

Because of this sadness, adding a name to globals() dictionary‑like container will IMPLICITLY make it an Optional type in Python's typing world.

I still think this is a bug mentally. It is not obvious. It is not clear. It is not really sensible. Yet, there it is.

The right thing to do would be to have the ability to immediately assign a value to a global at the same time it is made global and thus have a non‑Optional global type, and also remove the non‑obvious, confusing troublesome edge case this is. Typing is great and would be lovely to have more of it in Python. It helps scale, it helps remove logical errors and ambiguities that cause difficult bugs. It highlights when you have situations where you have not accounted for a variable possibly being a type wholly incompatible with the code written.

(by ThatXlinerblhsinguchuugaka)

參考文件

  1. Why can't an annotated variable be global? (CC BY‑SA 2.5/3.0/4.0)

#syntax-error #python-3.x #global-variables






相關問題

SQL Server 2008 - 嘗試編輯日期 - 語法錯誤? (SQL Server 2008 - Trying to edit dates - Syntax Error?)

將 Jquery.min 升級到 Jquery.1.9 時出錯 (Error Upgrade Jquery.min into Jquery.1.9)

SyntaxError:無效語法(打印功能) (SyntaxError: invalid syntax (print function))

錯誤:“if”之前的預期表達式 (error: expected expression before ‘if’)

導致 C2061 的用戶創建的標頭:語法錯誤:標識符“類名” (User-Created Header Causing C2061: Syntax Error : Identifier 'classname')

If 語句行收到語法錯誤 (Syntax Error received on If statement line)

語法錯誤:到達了意外的文件結尾。你有一個未關閉的#if (Syntax error: Unexpected end of file reached. You have an unclosed #if)

已經放 } 但錯誤仍然說 } 是預期的? (Already put } but the error still says that } is expected?)

為什麼 Java 不允許在這裡使用三元運算符? (Why doesn't Java allow the use of a ternary operator here?)

我在繪製向量時遇到語法錯誤 (I'm encountering a syntax error while plotting vectors)

我想在 Kotlin 中自定義新按鈕時遇到問題 (I have a problem when I want to customize my new button in Kotlin)

為什麼帶註釋的變量不能是全局的? (Why can't an annotated variable be global?)







留言討論