首页 > 编程语言 >Python 类别别名 type alias

Python 类别别名 type alias

时间:2023-07-12 17:45:21浏览次数:36  
标签:scale Tuple Python float alias Vector type Iterable

Type aliases

Type aliases are defined by simple variable assignments:

Url = str

def retry(url: Url, retry_count: int) -> None: ...

 

Note that we recommend capitalizing alias names, since they represent user-defined types, which (like user-defined classes) are typically spelled that way.

Type aliases may be as complex as type hints in annotations – anything that is acceptable as a type hint is acceptable in a type alias:

from typing import TypeVar, Iterable, Tuple

T = TypeVar('T', int, float, complex)
Vector = Iterable[Tuple[T, T]]

def inproduct(v: Vector[T]) -> T:
    return sum(x*y for x, y in v)
def dilate(v: Vector[T], scale: T) -> Vector[T]:
    return ((x * scale, y * scale) for x, y in v)
vec = []  # type: Vector[float]

 

This is equivalent to:

from typing import TypeVar, Iterable, Tuple

T = TypeVar('T', int, float, complex)

def inproduct(v: Iterable[Tuple[T, T]]) -> T:
    return sum(x*y for x, y in v)
def dilate(v: Iterable[Tuple[T, T]], scale: T) -> Iterable[Tuple[T, T]]:
    return ((x * scale, y * scale) for x, y in v)
vec = []  # type: Iterable[Tuple[float, float]]

ref: https://peps.python.org/pep-0484/#type-aliases

标签:scale,Tuple,Python,float,alias,Vector,type,Iterable
From: https://www.cnblogs.com/sakuraki/p/17548352.html

相关文章

  • Python面试
    1.了解哪些Python装饰器?@torch.no_grad():这个装饰器用于禁用梯度计算,以减少内存消耗和加速推断过程。@lru_cache 在记忆化搜索中保存历史结果。 2.GIL是什么GIL是CPython的一个特性,它限制了多线程的实现,单位时间只有一个线程能执行任务。但使用GIL可以更好的结合C++的......
  • wxpython重写统计图的工具栏的提示
    importwximportmatplotlibfrommatplotlib.figureimportFigurefrommatplotlib.backends.backend_wxaggimportFigureCanvasWxAggasFigureCanvasfrommatplotlib.backends.backend_wxaggimportNavigationToolbar2WxAggasNavigationToolbar#设置全局字体配置ma......
  • python实现ttl缓存
      importtimeimportfunctoolsimportthreadingdefttl_cache(func):cache={}lock=threading.Lock()@functools.wraps(func)defwrapper(*args,**kwargs):key=argsforiteminkwargs.items():key+=it......
  • PYTHON 函数 使用
    如何使用PYTHON里的ZIP函数a=["Peter","Ben","Alice","Jim"]b=["Apple","Banana","Pear","Orange"]新建两个列表,赋予不同的内容。pack=zip(a,b)print(list(pack))pack=zip(a,b)prin......
  • python实现两函数通过缩放,平移和旋转进行完美拟合
    Curve_fitting前几天在工作的时候接到了一个需求,希望将不同坐标系,不同角度的两条不规则曲线,并且组成该曲线的点集数量不一致,需求是希望那个可以通过算法的平移和旋转搞到一个概念里最贴合,拟合态进行比较。这是初步将两组数据画到图里的情况,和背景需求是一致的。其实从肉眼看过......
  • python pip安装使用
    安装了python,没安装pip,在pycharm中执行pip命令会报错:py:无法将“py”项识别为cmdlet、函数、脚本文件或可运行程序的名称。请检查名称的拼写,如果包括路径,请确保路径正确,然后再试一次。首先需要安装pip下载pip并解压到本地:https://pypi.org/project/pip/#files,window我下载......
  • Python 量化投资(一):滑动均值、布林带、MACD、RSI、KDJ、OBV
    滑动均值和标准差为了更好利用向量化来加速,滑动窗口使用np.lib.stride_tricks.sliding_window_view(x,win)提取,它会返回所有x[i]开头并且长度为win的数组的数组。defrolling(x,win):r=np.lib.stride_tricks.sliding_window_view(x,win)pad=np.zeros([len(x)-......
  • python魔术方法之__new__
    一、基本用法#从一个类建立一个对象#__new__从class建立一个object过程#__init__有了object初始化过程classLanguage:def__new__(cls,*args,**kwargs):print("__new__")returnsuper().__new__(cls)def__init__(self):print(......
  • 【Python】对密码文本进行加密, 并判断 hashlib
    importhashlibdefencrypt_password(password,salt):#创建一个sha256的哈希对象sha256_hash=hashlib.sha256()#将盐值和密码组合起来并进行哈希hashed_password=salt.encode('utf-8')+password.encode('utf-8')sha256_hash.update(hashed_......
  • 5python学习笔记
    1.python特点​Python具有代码简单、学习难度低、语法清楚、功能库丰富等优势,同样功能的代码,Python代码数量只有C或Java的1/5,甚至1/10。例:打印HelloWorld,C语言需要6行,Java需要5行,Python只需要1行。2.python相关概念第三方库:需要自行安装的库python解释器:将源代码翻译......