简体   繁体   中英

What's the difference between .union and | for sets in python?

What's the difference between .union and | for sets in python?

>>> a = set([1, 2, 3, 4])
>>> b = set([3, 4, 5, 6])

>>> a|b
{1, 2, 3, 4, 5, 6}

>>> a.union(b)
{1, 2, 3, 4, 5, 6}

No difference.

In fact on the official python documentation about sets they are written together.

There is a little difference: one is an operator, so it has specific operator the operator precedence (eg if mixed with other set operators). On the function case, the function parenthesis explicitly fix the priority.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM