首页 > 编程语言 >forward reference in python

forward reference in python

时间:2024-03-10 10:59:01浏览次数:28  
标签:name reference quotes python Python defined forward type

Forward Reference in python

There is a code snippet like:

@property
def analyses(self) -> "AnalysesHubWithDefault":
    result = self._analyses
    if result is None:
        raise ValueError("Cannot access analyses this early in project lifecycle")
    return result

In Python, forward references are addressed using string literals for type hints. This is necessary when you reference a class that is not yet defined at the point where you're using it. This situation often occurs in cases of circular dependencies or when the type hint is for a class that is defined later in the same module.

The reason AnalysesHubWithDefault is enclosed in quotes ("") in the method signature you provided is to indicate that it is a forward reference. This is a way to tell Python: "This name will be defined later, so treat it as the name of a class for type hinting purposes."

Python's type hinting system, introduced in PEP 484, allows for this usage to enable more complex type hints without running into issues with names that are not yet defined. Without the use of quotes, Python would raise a NameError because it tries to evaluate the name as a reference to a class or type object when parsing the function definition. By using quotes, you defer the resolution of the name until later, allowing for the type checker (like mypy or PyCharm's built-in type checker) to resolve the name after all classes have been defined.

As of Python 3.7, with the introduction of the from future import annotations statement (PEP 563), all annotations are stored as string literals by default, which defers the evaluation of type annotations. This means that in Python 3.7 and later, you could write the type hint without quotes if you have the future import at the top of your module:

from __future__ import annotations

def analyses(self) -> AnalysesHubWithDefault:
    ...

Reference

标签:name,reference,quotes,python,Python,defined,forward,type
From: https://www.cnblogs.com/holylandofdora/p/18063814

相关文章

  • 【Python使用】python高级进阶知识md总结第2篇:HTTP 请求报文,HTTP响应报文【附代码文
    python高级进阶全知识知识笔记总结完整教程(附代码资料)主要内容讲述:操作系统,虚拟机软件,Ubuntu操作系统,Linux内核及发行版,查看目录命令,切换目录命令,绝对路径和相对路径,创建、删除文件及目录命令,复制、移动文件及目录命令,终端命令格式的组成,查看命令帮助。HTTP请求报文,HTTP响应报文......
  • 解决python导入csv文件报错
    python编码报错:UnicodeDecodeError:‘utf-8‘codeccan‘tdecodebyte0xbcinposition2:invalidstartbyt_unicodedecodeerror:'utf-8'codeccan'tdecodebyt-CSDN博客报错原因是:UnicodeDecodeError:'utf-8'codeccan'tdecodebyte0xb5in......
  • Python 潮流周刊第 41 期(摘要),赠书5本
    本周刊由Python猫出品,精心筛选国内外的250+信息源,为你挑选最值得分享的文章、教程、开源项目、软件工具、播客和视频、热门话题等内容。愿景:帮助所有读者精进Python技术,并增长职业和副业的收入。周刊全文:https://pythoncat.top/posts/2024-03-09-weekly《Python工匠》专注......
  • Python基于TCP实现聊天功能
    Server端importsocketimportqueueimportthreadingimporttimeserversocket=socket.socket(socket.AF_INET,socket.SOCK_STREAM)host=socket.gethostname()print("服务器IP:"+socket.gethostbyname(host))serversocket.bind((host,9090))serversock......
  • python Ai 应用开发基础训练,字符串,字典,文件
    --------------------------------------  编程能是大模型应用的天花板..................................................................所以要好好将大模型应用在企业一定要好好练好最看不起的一环,基础能力字符串处理 本文档来自老男孩培训Alex课程记录,我在2017年......
  • python+pytest接口自动化之测试函数、测试类/测试方法的封装
    前言今天呢,笔者想和大家聊聊python+pytest接口自动化中将代码进行封装,只有将测试代码进行封装,才能被测试框架识别执行。例如单个接口的请求代码如下:importrequestsheaders={"user-agent":"Mozilla/5.0(WindowsNT10.0;Win64;x64)AppleWebKit/537.36(KHTML,l......
  • k8s中port-forward 与service的nodeport区别
    在Kubernetes中,port-forward和Service的NodePort是两种将外部流量引入集群内部Pod的方法,但它们在实现方式、使用场景和安全性等方面有所不同。port-forward:实现方式:kubectlport-forward命令允许你将本地机器上的一个端口转发到Kubernetes集群中的一个Pod上的端口。这是一种......
  • 【Python使用】python高级进阶知识md总结第1篇:My Awesome Book【附代码文档】
    python高级进阶全知识知识笔记总结完整教程(附代码资料)主要内容讲述:MyAwesomeBook,MyAwesomeBook。MyAwesomeBook,MySQL数据库。MyAwesomeBook,聚合函数。MyAwesomeBook,创建表并给某个字段添加数据。MyAwesomeBook,闭包。MyAwesomeBook,路由列表功能开发。MyAwesomeBoo......
  • vscode编写python
    安装插件打开cmd创建虚拟环境C:\Users\ychen>condacreate-nenv_devpython=3.10.4Fetchingpackagemetadata.................Solvingpackagespecifications:.PackageplanforinstallationinenvironmentC:\ProgramData\Anaconda3\envs\env_dev:Thefo......
  • python面向对象(一)
    面向对象(ObjectOrientedProgram)在Python中,所有的数据(包括数字和字符串)实际都是对象,同一类型的对象都有相同的类型。我们可以使用type()函数来获取关于对象的类型信息。什么是类,什么是对象?在Python中,使用类来定义同一种类型的对象。类(class)是广义的数据类型,能够定义复......