* on python是什么意思?

阿德里安·塞纳(Adrian Serna)

我有下一个查询。

item = [item.export_simple()
            for item in session.query(Item)
                               .filter(and_(
                                       Item.companyId == company_id,
                                       or_(
                                           True if search == "" else None,
                                           or_(*[Item.name.like('%{0}%'.format(s)) for s in words]),
                                           or_(*[Item.code.like('%{0}%'.format(s)) for s in words])
                                            ))).order_by(Item.name)]

还有这个。

if type == "code":
            src = [Item.code.like('%{0}%'.format(s)) for s in words]
        elif type == "name":
            src = [Item.name.like('%{0}%'.format(s)) for s in words]

 session.query(Item)
                    .filter(and_(
                        Item.companyId == company_id,
                        Item.typeItem == item_type,
                        or_(
                            True if search == "" else None,
                            or_(*src)
                        )))

在这两种情况下,我*在or_()语句中都有运算符,并且两个查询都很棒,但是我不知道为什么。这是参考这一个

亚伦·克里斯蒂安森(Aaron Christiansen)

这与SQLAlchemy无关。这会将列表解压缩为逗号分隔的参数,并且是Python的一项出色功能。正式将其称为“单星”运算符,但通常将其称为“ splat”运算符。这:

a = [1, 2, 3]
something(*a)

等效于此:

something(1, 2, 3)

因此,在您的第一个示例中,它正在执行列表推导[Item.name.like('%{0}%'.format(s)) for s in words],然后将其参数打包到or_调用中。

有关此运算符的更多信息,请参阅Python文档

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章