*在python上是什么意思?

Adr*_*rna 1 python sqlalchemy

我有下一个查询.

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)]
Run Code Online (Sandbox Code Playgroud)

还有这个.

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)
                        )))
Run Code Online (Sandbox Code Playgroud)

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

Aar*_*sen 5

这与SQLAlchemy无关.这将列表解压缩为逗号分隔的参数,并且是一个出色的Python功能.它被正式称为"单星"运算符,但它通常被称为"splat"运算符.这个:

a = [1, 2, 3]
something(*a)
Run Code Online (Sandbox Code Playgroud)

相当于:

something(1, 2, 3)
Run Code Online (Sandbox Code Playgroud)

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

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


归档时间:

查看次数:

88 次

最近记录:

8 年,2 月 前