Python 3.x从基础到实战 - typing类型提示

Python Typing模块是Python 3.5版本之后才引入的模块,它提供了一些类型注解的功能,可以帮助开发者更好地进行类型检查和类型提示。在Python中,类型注解并不是强制要求的,但是使用类型注解能够提高代码的可读性和可维护性,特别是在大型项目中。

本教程将介绍Python Typing模块的基础用法和进阶用法,并提供一些用法示例。

基础用法

基本类型注解

Python Typing模块中包含了许多基本类型,包括:

  • int
  • float
  • str
  • bool
  • list
  • tuple
  • set
  • dict

可以通过在函数参数或返回值前添加类型注解来指定参数或返回值的类型。示例代码如下:

def add(x: int, y: int) -> int:
    return x + y

def concat(s1: str, s2: str) -> str:
    return s1 + s2

在上面的示例中,add函数接受两个整数类型的参数,并返回一个整数类型的值;concat函数接受两个字符串类型的参数,并返回一个字符串类型的值。

Union类型注解

有时候一个参数或返回值可能有多种类型,可以使用Union类型注解来指定。示例代码如下:

from typing import Union

def add(x: Union[int, float], y: Union[int, float]) -> Union[int, float]:
    return x + y

在上面的示例中,add函数接受两个参数,这两个参数可以是整数类型或浮点数类型,返回值也可以是整数类型或浮点数类型。

List、Tuple和Set类型注解

可以使用List、Tuple和Set类型注解来指定一个列表、元组或集合的元素类型。示例代码如下:

from typing import List, Tuple, Set

def process_list(lst: List[int]) -> List[int]:
    return [x * 2 for x in lst]

def process_tuple(tup: Tuple[str, int, float]) -> Tuple[float, str, int]:
    return (tup[2], tup[0], tup[1])
Forcing web requests to allow TLS v1.2 (Required for requests to Chocolatey.org) Getting latest version of the Chocolatey package for download. Not using proxy. Getting Chocolatey from https://community.chocolatey.org/api/v2/package/chocolatey/2.5.1. Downloading https://community.chocolatey.org/api/v2/package/chocolatey/2.5.1 to C:\Users\Administrator\AppData\Local\Temp\chocolatey\chocoInstall\chocolatey.zip Not using proxy. Extracting C:\Users\Administrator\AppData\Local\Temp\chocolatey\chocoInstall\chocolatey.zip to C:\Users\Administrator\AppData\Local\Temp\chocolatey\chocoInstall Installing Chocolatey on the local machine Creating ChocolateyInstall as an environment variable (targeting 'Machine') Setting ChocolateyInstall to 'C:\ProgramData\chocolatey' WARNING: It's very likely you will need to close and reopen your shell before you can use choco. Restricting write permissions to Administrators We are setting up the Chocolatey package repository. The packages themselves go to 'C:\ProgramData\chocolatey\lib' (i.e. C:\ProgramData\chocolatey\lib\yourPackageName). A shim file for the command line goes to 'C:\ProgramData\chocolatey\bin' and points to an executable in 'C:\ProgramData\chocolatey\lib\yourPackageName'. Creating Chocolatey CLI folders if they do not already exist. chocolatey.nupkg file not installed in lib. Attempting to locate it from bootstrapper. PATH environment variable does not have C:\ProgramData\chocolatey\bin in it. Adding... 警告: Not setting tab completion: Profile file does not exist at 'C:\Users\Administrator\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1'. Chocolatey CLI (choco.exe) is now ready. You can call choco from anywhere, command line or PowerShell by typing choco. Run choco /? for a list of functions. You may need to shut down and restart PowerShell and/or consoles first prior to using choco. Ensuring Chocolatey commands are on the path Ensuring chocolatey.nupkg is in the lib folder Chocolatey v2.5.1 Upgrading the following packages: python;visualstudio2019-workload-vctools By upgrading, you accept licenses for the packages. python is not installed. Installing... Downloading package from source 'https://community.chocolatey.org/api/v2/' Progress: Downloading chocolatey-compatibility.extension 1.0.0... 100% chocolatey-compatibility.extension v1.0.0 [Approved] chocolatey-compatibility.extension package files upgrade completed. Performing other installation steps. Installed/updated chocolatey-compatibility extensions. The upgrade of chocolatey-compatibility.extension was successful. Deployed to 'C:\ProgramData\chocolatey\extensions\chocolatey-compatibility' Downloading package from source 'https://community.chocolatey.org/api/v2/' Progress: Downloading chocolatey-core.extension 1.4.0... 100% chocolatey-core.extension v1.4.0 [Approved] chocolatey-core.extension package files upgrade completed. Performing other installation steps. Installed/updated chocolatey-core extensions. The upgrade of chocolatey-core.extension was successful. Deployed to 'C:\ProgramData\chocolatey\extensions\chocolatey-core' Downloading package from source 'https://community.chocolatey.org/api/v2/' Progress: Downloading chocolatey-windowsupdate.extension 1.0.5... 100% chocolatey-windowsupdate.extension v1.0.5 [Approved] chocolatey-windowsupdate.extension package files upgrade completed. Performing other installation steps. Installed/updated chocolatey-windowsupdate extensions. The upgrade of chocolatey-windowsupdate.extension was successful. Deployed to 'C:\ProgramData\chocolatey\extensions\chocolatey-windowsupdate' Downloading package from source 'https://community.chocolatey.org/api/v2/' Progress: Downloading KB2919442 1.0.20160915... 100% KB2919442 v1.0.20160915 [Approved] KB2919442 package files upgrade completed. Performing other installation steps. Skipping installation because this hotfix only applies to Windows 8.1 and Windows Server 2012 R2. The upgrade of KB2919442 was successful. Software install location not explicitly set, it could be in package or default install location of installer. Downloading package from source 'https://community.chocolatey.org/api/v2/' Progress: Downloading KB2919355 1.0.20160915... 100% KB2919355 v1.0.20160915 [Approved] KB2919355 package files upgrade completed. Performing other installation steps. Skipping installation because this hotfix only applies to Windows 8.1 and Windows Server 2012 R2. The upgrade of KB2919355 was successful. Software install location not explicitly set, it could be in package or default install location of installer. Downloading package from source 'https://community.chocolatey.org/api/v2/' Progress: Downloading KB2999226 1.0.20181019... 100% KB2999226 v1.0.20181019 [Approved] KB2999226 package files upgrade completed. Performing other installation steps. Skipping installation because update KB2999226 does not apply to this operating system (Microsoft Windows 10 专业版). The upgrade of KB2999226 was successful. Software install location not explicitly set, it could be in package or default install location of installer. Downloading package from source 'https://community.chocolatey.org/api/v2/' Progress: Downloading KB3035131 1.0.3... 100% KB3035131 v1.0.3 [Approved] KB3035131 package files upgrade completed. Performing other installation steps. Skipping installation because update KB3035131 does not apply to this operating system (Microsoft Windows 10 专业版). The upgrade of KB3035131 was successful. Software install location not explicitly set, it could be in package or default install location of installer. Downloading package from source 'https://community.chocolatey.org/api/v2/' Progress: Downloading KB3033929 1.0.5... 100% KB3033929 v1.0.5 [Approved] KB3033929 package files upgrade completed. Performing other installation steps. Skipping installation because update KB3033929 does not apply to this operating system (Microsoft Windows 10 专业版). The upgrade of KB3033929 was successful. Software install location not explicitly set, it could be in package or default install location of installer. Downloading package from source 'https://community.chocolatey.org/api/v2/' Progress: Downloading vcredist140 14.44.35211... 100% vcredist140 v14.44.35211 [Approved] vcredist140 package files upgrade completed. Performing other installation steps. Downloading vcredist140-x86 from 'https://download.visualstudio.microsoft.com/download/pr/7ebf5fdb-36dc-4145-b0a0-90d3d5990a61/0C09F2611660441084CE0DF425C51C11E147E6447963C3690F97E0B25C55ED64/VC_redist.x86.exe' Progress: 100% - Completed download of C:\Users\Administrator\AppData\Local\Temp\chocolatey\vcredist140\14.44.35211\VC_redist.x86.exe (13.31 MB). Download of VC_redist.x86.exe (13.31 MB) completed. Hashes match. Installing vcredist140-x86... vcredist140-x86 has been installed. Downloading vcredist140-x64 64 bit from 'https://download.visualstudio.microsoft.com/download/pr/7ebf5fdb-36dc-4145-b0a0-90d3d5990a61/CC0FF0EB1DC3F5188AE6300FAEF32BF5BEEBA4BDD6E8E445A9184072096B713B/VC_redist.x64.exe' Progress: 100% - Completed download of C:\Users\Administrator\AppData\Local\Temp\chocolatey\vcredist140\14.44.35211\VC_redist.x64.exe (24.45 MB). Download of VC_redist.x64.exe (24.45 MB) completed. Hashes match. Installing vcredist140-x64... vcredist140-x64 has been installed. vcredist140 may be able to be automatically uninstalled. The upgrade of vcredist140 was successful. Software installed as 'exe', install location is likely default. Downloading package from source 'https://community.chocolatey.org/api/v2/' Progress: Downloading vcredist2015 14.0.24215.20170201... 100% vcredist2015 v14.0.24215.20170201 [Approved] vcredist2015 package files upgrade completed. Performing other installation steps. The upgrade of vcredist2015 was successful. Deployed to 'C:\ProgramData\chocolatey\lib\vcredist2015' Downloading package from source 'https://community.chocolatey.org/api/v2/' Progress: Downloading python313 3.13.7... 100% python313 v3.13.7 [Approved] python313 package files upgrade completed. Performing other installation steps. Installing 64-bit python313... python313 has been installed. Added C:\ProgramData\chocolatey\bin\python3.13.exe shim pointed to 'c:\python313\python.exe'. Python installed to: 'C:\Python313' Restricting write permissions to Administrators python313 can be automatically uninstalled. Environment Vars (like PATH) have changed. Close/reopen your shell to see the changes (or in powershell/cmd.exe just type `refreshenv`). The upgrade of python313 was successful. Software installed as 'exe', install location is likely default. Downloading package from source 'https://community.chocolatey.org/api/v2/' Progress: Downloading python3 3.13.7... 100% python3 v3.13.7 [Approved] python3 package files upgrade completed. Performing other installation steps. The upgrade of python3 was successful. Deployed to 'C:\ProgramData\chocolatey\lib\python3' Downloading package from source 'https://community.chocolatey.org/api/v2/' Progress: Downloading python 3.13.7... 100% python v3.13.7 [Approved] python package files upgrade completed. Performing other installation steps. The upgrade of python was successful. Deployed to 'C:\ProgramData\chocolatey\lib\python' visualstudio2019-workload-vctools is not installed. Installing... Downloading package from source 'https://community.chocolatey.org/api/v2/' Progress: Downloading chocolatey-dotnetfx.extension 1.0.1... 100% chocolatey-dotnetfx.extension v1.0.1 chocolatey-dotnetfx.extension package files upgrade completed. Performing other installation steps. Installed/updated chocolatey-dotnetfx extensions. The upgrade of chocolatey-dotnetfx.extension was successful. Deployed to 'C:\ProgramData\chocolatey\extensions\chocolatey-dotnetfx' Downloading package from source 'https://community.chocolatey.org/api/v2/' Progress: Downloading chocolatey-visualstudio.extension 1.11.1... 100% chocolatey-visualstudio.extension v1.11.1 chocolatey-visualstudio.extension package files upgrade completed. Performing other installation steps. Installed/updated chocolatey-visualstudio extensions. The upgrade of chocolatey-visualstudio.extension was successful. Deployed to 'C:\ProgramData\chocolatey\extensions\chocolatey-visualstudio' Downloading package from source 'https://community.chocolatey.org/api/v2/' Progress: Downloading dotnetfx 4.8.0.20220524... 100% dotnetfx v4.8.0.20220524 [Approved] dotnetfx package files upgrade completed. Performing other installation steps. Microsoft .NET Framework 4.8 or later is already installed. The upgrade of dotnetfx was successful. Software install location not explicitly set, it could be in package or default install location of installer. Downloading package from source 'https://community.chocolatey.org/api/v2/' Progress: Downloading visualstudio-installer 2.0.3... 100% visualstudio-installer v2.0.3 [Approved] visualstudio-installer package files upgrade completed. Performing other installation steps. The upgrade of visualstudio-installer was successful. Software install location not explicitly set, it could be in package or default install location of installer. Downloading package from source 'https://community.chocolatey.org/api/v2/' Progress: Downloading visualstudio2019buildtools 16.11.49... 100% visualstudio2019buildtools v16.11.49 [Approved] visualstudio2019buildtools package files upgrade completed. Performing other installation steps. Downloading channel manifest from 'https://aka.ms/vs/16/release/channel' Progress: 100% - Completed download of C:\Users\Administrator\AppData\Local\Temp\chocolatey\chocolatey-visualstudio.extension\ChannelManifest_-602808024.man (131.59 KB). Download of ChannelManifest_-602808024.man (131.59 KB) completed. Downloading catalog manifest from 'https://download.visualstudio.microsoft.com/download/pr/7bbc1e39-eb50-4636-badd-9ac799f48c9c/bba435ef3a199b80129e67f8a051700394f221b2bf1ae63eaa921c15a32b91b4/VisualStudio.vsman' Progress: 100% - Completed download of C:\Users\Administrator\AppData\Local\Temp\chocolatey\chocolatey-visualstudio.extension\Catalog_85309983.man (10.66 MB). Download of Catalog_85309983.man (10.66 MB) completed. Downloading visualstudio2019buildtools from 'https://download.visualstudio.microsoft.com/download/pr/12569159-fcaa-4029-a83e-b7b900c5f9d6/2801bffe21314de3cbd5e0dd600c376d5eaa38a18a738b61453a8cc28eff8690/vs_BuildTools.exe' Progress: 100% - Completed download of C:\Users\Administrator\AppData\Local\Temp\chocolatey\visualstudio2019buildtools\16.11.49\vs_BuildTools.exe (4.22 MB). Download of vs_BuildTools.exe (4.22 MB) completed. Hashes match. Installing visualstudio2019buildtools...
最新发布
09-21
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值