簡體   English   中英

Python 3.7 與 2.7,分配

[英]Python 3.7 vs 2.7, assignment

我有點像 2.7 Python 的人。 我遇到了這段代碼,我可以假設它做了什么,但它到底是什么意思? Model is used, but I assume that model is a variable that is of the type of class, but then we assign a function to it. 我覺得很奇怪。

model: Class = a_function(f)

以上是什么意思?

這是一個類型注釋 它實際上並沒有在代碼中任何事情,但是注釋可以被第三方應用程序(例如 mypy)讀取以執行類型檢查。 在您的情況下,代碼聲明變量model的類型是Class

表達式的 rest 是正則賦值。

您發布的以下聲明:

model: Class = a_function(f)

表示使用其類型聲明的變量。 Python 中不需要此規則,但它是一種顯示正在使用的參數或變量類型的方法。 該屬性稱為“ type_annotation ”。 例如,在復雜的函數實現中,它可能很有用,例如以下代碼:

def process_some_data(client: str, properties: dict, feature: SomeClass):
   # some  code
   return something

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM