python typing.Literal 与sqlalchemy中的Literal

文章介绍了Python中的typing.Literal类型注解,它用于限制函数参数只能接受预设的特定值,如1,2,3,起到类似枚举的作用。此外,还提及了sqlalchemy库中的literal元素,但未详细展开。
部署运行你感兴趣的模型镜像

python typing.Literal是用于限制函数传参,例如:

from typing import Literal

def func(a:Literal[1,2,3]):

print(a)

那么这个函数传参只能选择1,2,3 。类似一种枚举

sqlalchemy中的literal

用于在sql查询时添加常量作为新的一列,例如:

from sqlalchemy.sql.elements import literal

sql =session.query(User.name, User.fullname,literal("张三").label("列名"))

print(sql.str())

-------------------------------->

select name, fullname, '张三' as "列名" from user;

您可能感兴趣的与本文相关的镜像

Python3.8

Python3.8

Conda
Python

Python 是一种高级、解释型、通用的编程语言,以其简洁易读的语法而闻名,适用于广泛的应用,包括Web开发、数据分析、人工智能和自动化脚本

Traceback (most recent call last): File "/home/junsi/anaconda3/lib/python3.11/typing.py", line 857, in __init__ code = compile(arg_to_compile, '<string>', 'eval') ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "<string>", line 1 mysql+aiomysql://root:123.com@10.8.0.2/prison ^ SyntaxError: invalid decimal literal During handling of the above exception, another exception occurred: Traceback (most recent call last): File "/home/junsi/code/integration-platform/module/main/new.py", line 73, in <module> class DatabaseSettings(BaseModel): File "/home/junsi/code/integration-platform/module/main/new.py", line 84, in DatabaseSettings def get_dburl(self) -> Union[str, URL]: ~~~~~^^^^^^^^^^ File "/home/junsi/anaconda3/lib/python3.11/typing.py", line 355, in inner return cached(*args, **kwds) ^^^^^^^^^^^^^^^^^^^^^ File "/home/junsi/anaconda3/lib/python3.11/typing.py", line 481, in __getitem__ return self._getitem(self, parameters) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/junsi/anaconda3/lib/python3.11/typing.py", line 694, in Union parameters = tuple(_type_check(p, msg) for p in parameters) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/junsi/anaconda3/lib/python3.11/typing.py", line 694, in <genexpr> parameters = tuple(_type_check(p, msg) for p in parameters) ^^^^^^^^^^^^^^^^^^^ File "/home/junsi/anaconda3/lib/python3.11/typing.py", line 186, in _type_check arg = _type_convert(arg, module=module, allow_special_forms=allow_special_forms) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/junsi/anaconda3/lib/python3.11/typing.py", line 164, in _type_convert return ForwardRef(arg, module=module, is_class=allow_special_forms) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/junsi/anaconda3/lib/python3.11/typing.py", line 859, in __init__ raise SyntaxError(f"Forward reference must be an expression -- got {arg!r}") SyntaxError: Forward reference must be an expression -- got 'mysql+aiomysql://root:123.com@10.8.0.2/prison'
06-19
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值