Josh Rosenberg <shadowranger+pyt...@gmail.com> added the comment:

set.union() without constructing the set you call union on only happens to work 
for the set.union(a) case because `a` is already a set. union takes arbitrary 
iterables, not just sets, and you're just cheating by explicitly passing `a` as 
the expected self argument. If you'd set `a = [1, 2]` (a list, not a set), 
set.union(a) would fail, because set.union(a) was only working by accident of a 
being interpreted as self; any such use is misuse.

Point is, the zero args case isn't a unique corner case;

args = ([1, 2], ANY OTHER ITERABLES HERE)
set.union(*args)

fails too, because the first argument is interpreted as self, and must be a set 
for this to work.

SilentGhost's solution of constructing the set before union-ing via 
set().union(*args) is the correct solution; it's free of corner cases, removing 
the specialness of the first element in args (because self is passed in 
correctly), and not having any troubles with empty args.

intersection is the only interesting case here, where preconstruction of the 
empty set doesn't work, because that would render the result the empty set 
unconditionally. The solution there is set(args[0]).intersection(*args) (or 
*args[1:]), but that's obviously uglier.

I'm -1 on making any changes to set.union to support this misuse case.

----------
nosy: +josh.r

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue35338>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to