有人可以用Laravel 5.3中的ajax post方法解释一个完整的最小例子吗?我知道网上有一些资源,但我想念一个简洁,直截了当的最小例子.
是否typing.TypedDict允许额外的钥匙?如果某个值具有 TypedDict 定义中不存在的键,该值是否会通过类型检查器?
Python 中的逻辑运算符是惰性的。具有以下定义:
def func(s):
print(s)
return True
Run Code Online (Sandbox Code Playgroud)
呼叫or接线员
>>> func('s') or func('t')
's'
Run Code Online (Sandbox Code Playgroud)
只计算第一个函数调用,因为or识别表达式计算为
True,而不管第二个函数调用的返回值。and行为类似。
但是,当以下列方式使用any()(类比:)时all():
>>> any([func('s'), func('t')])
's'
't'
Run Code Online (Sandbox Code Playgroud)
所有函数调用都会被评估,因为在any开始迭代其项目的布尔值之前,首先构造内部列表。当我们省略列表构造而只写时,也会发生同样的情况
>>> func('s') or func('t')
's'
Run Code Online (Sandbox Code Playgroud)
这样,我们失去的力量any是短路,它只要迭代的第一个元素是truish打破该装置。如果函数调用代价高昂,那么预先评估所有函数是一个很大的损失,并且浪费了any. 从某种意义上说,人们可以将其称为 Python 陷阱,因为对于尝试利用 的此功能的用户来说可能出乎意料any,并且any通常被认为只是链接一系列or语句的另一种语法方式。但any只是短路,而不是懒惰,这就是这里的区别。
any正在接受一个 iterable。因此,应该有一种创建迭代器的方法,该迭代器不会预先评估其元素,而是将未评估的元素传递给any并让它们any仅在内部评估,以实现完全懒惰的评估。
所以,问题是:我们如何使用any真正的惰性函数评估?这意味着:我们如何制作一个any可以消费的函数调用迭代器,而无需预先评估所有函数调用?
I have a function which validates its argument to accept only values from a given list of valid options. Typing-wise, I reflect this behavior using a Literal type alias, like so:
from typing import Literal
VALID_ARGUMENTS = ['foo', 'bar']
Argument = Literal['foo', 'bar']
def func(argument: 'Argument') -> None:
if argument not in VALID_ARGUMENTS:
raise ValueError(
f'argument must be one of {VALID_ARGUMENTS}'
)
# ...
Run Code Online (Sandbox Code Playgroud)
这违反了 DRY 原则,因为我必须在我的 Literal 类型的定义中重写有效参数列表,即使它已经存储在变量 中VALID_ARGUMENTS。给定变量,如何Argument动态创建Literal 类型VALID_ARGUMENTS …
Python 是否有一个类似于列表理解的结构来创建字符串,即“字符串理解”?
我需要做的任务是删除字符串中的所有标点符号。
def remove_punctuation(text: str) -> str:
punctuations = [",", ".", "!", "?"]
# next line is what I want to do
result = text.replace(p, "") for p in punctuations
return result
assert remove_punctuation("A! Lion?is. crying!..") == "A Lion is crying"
Run Code Online (Sandbox Code Playgroud) 我想检查函数的参数是否可下标。我如何使用Python的typing模块来做到这一点?
我搜索了文档但没有找到任何东西。但也许可以创建自定义Type. 我该怎么做呢?
我想将方法的参数键入为一组有限的有效值之一。所以基本上,我想要typing等效于以下最小示例:
valid_parameters = ["value", "other value"]
def typed_method(parameter):
if not parameter in valid_parameters:
raise ValueError("invalid parameter")
Run Code Online (Sandbox Code Playgroud)
我typing已经检查过了,但我没有设法找到解决方案。也许我只是无法完全理解文档。有这样的解决方案吗?可以创建吗?