我可以依赖在传入 python 之前评估的参数吗?
Can I rely on arguments being evaluated before being passed in python?
我正在编写一个基本的模拟框架,以便我可以测试对 peewee 的一些调用。
我知道在 peewee,你可以拨打 where()
电话,例如
model.select().where(model.id == target_id). ...
为了检测正在比较模型的哪些字段,我将覆盖该字段的比较运算符。然后我会注入那个模型来代替 peewee 模型。它将提供与 peewee 模型相同的界面(不过只是根据需要提供),但它不会访问数据库,而是记录比较和调用;但这完全取决于在调用方法之前评估的参数。
我有一个使用 anaconda 的例子 运行,对于 python 随 Linux Mint 提供的任何东西,它们似乎都按照我的意愿运行。我不确定这是 运行 时间的侥幸,还是 python 确实在需要比较之前评估比较。
那么,关于以下内容,我可以依靠 model.id == target_id, ...
运行 在调用第一个 where()
之前进行比较吗?我可以依靠 model.name == target_name
运行 在调用第二个 where()
之前和调用第一个 where()
之后进行比较吗?
mockModel.select()
.where(model.id == target_id, model.number == target_number)
.where(model.name == target_name)
...
是的。来自 docs:
The primary must evaluate to a callable object (user-defined functions, built-in functions, methods of built-in objects, class objects, methods of class instances, and all objects having a __call__()
method are callable). All argument expressions are evaluated before the call is attempted.
方法参数在为调用编组时已解析。由于调用第一个 where
时尚未查找第二个 where
,因此尚未解析其参数。此语句的顺序
mockModel.select()\
.where(model.id == target_id, model.number == target_number)\
.where(model.name == target_name)
是
- 查找
mockModel
- 在该对象上查找
select
- 它没有参数,所以调用它并得到它的结果对象
- 在结果对象上查找
where
- 它是一个函数,所以从左到右计算它的参数
- 调用函数并获取其结果对象
- 在结果对象上查找
where
- 它是一个函数调用,所以从左到右计算它的参数
- 调用函数
我正在编写一个基本的模拟框架,以便我可以测试对 peewee 的一些调用。
我知道在 peewee,你可以拨打 where()
电话,例如
model.select().where(model.id == target_id). ...
为了检测正在比较模型的哪些字段,我将覆盖该字段的比较运算符。然后我会注入那个模型来代替 peewee 模型。它将提供与 peewee 模型相同的界面(不过只是根据需要提供),但它不会访问数据库,而是记录比较和调用;但这完全取决于在调用方法之前评估的参数。
我有一个使用 anaconda 的例子 运行,对于 python 随 Linux Mint 提供的任何东西,它们似乎都按照我的意愿运行。我不确定这是 运行 时间的侥幸,还是 python 确实在需要比较之前评估比较。
那么,关于以下内容,我可以依靠 model.id == target_id, ...
运行 在调用第一个 where()
之前进行比较吗?我可以依靠 model.name == target_name
运行 在调用第二个 where()
之前和调用第一个 where()
之后进行比较吗?
mockModel.select()
.where(model.id == target_id, model.number == target_number)
.where(model.name == target_name)
...
是的。来自 docs:
The primary must evaluate to a callable object (user-defined functions, built-in functions, methods of built-in objects, class objects, methods of class instances, and all objects having a
__call__()
method are callable). All argument expressions are evaluated before the call is attempted.
方法参数在为调用编组时已解析。由于调用第一个 where
时尚未查找第二个 where
,因此尚未解析其参数。此语句的顺序
mockModel.select()\
.where(model.id == target_id, model.number == target_number)\
.where(model.name == target_name)
是
- 查找
mockModel
- 在该对象上查找
select
- 它没有参数,所以调用它并得到它的结果对象
- 在结果对象上查找
where
- 它是一个函数,所以从左到右计算它的参数
- 调用函数并获取其结果对象
- 在结果对象上查找
where
- 它是一个函数调用,所以从左到右计算它的参数
- 调用函数