Is powerset function recipe name correct?

In Itertools Recipes there’s a function named powerset:

def powerset(iterable):
    "Subsequences of the iterable from shortest to longest."
    # powerset([1,2,3]) → () (1,) (2,) (3,) (1,2) (1,3) (2,3) (1,2,3)
    s = list(iterable)
    return chain.from_iterable(combinations(s, r) for r in range(len(s)+1))

The docstring is correct, but the name of the function is not. A power set of a set S is a set of all subsets of S.

So IMO the function should be renamed, or rewritten to:

def powerset(iterable):
    s = set(iterable)
    
    return chain.from_iterable(map(
        lambda x: frozenset(x), 
        combinations(s, r)) for r in range(len(s) + 1)
    )

Maybe open an issue?

2 Likes

For most practical purposes, the version in the documentation is likely to be better than one that forces everything to be a (frozen) set. The existing version gives useful results when faced with iterables of non-hashable values, or iterables containing duplicates (particularly when the duplicates are distinct objects that compare equal).

It’s easy to get the frozenset version from the published version, and impossible to go in the other direction.

So that only leaves renaming, and I don’t think there’s a good alternative name. Most people won’t care that the current name is not mathematically accurate - we’re writing programs, not doing maths. After all, a mathematical tuple isn’t the same as a Python tuple either. And floats aren’t real numbers.

If it matters to you, use a different name when you implement the recipe.

16 Likes

I don’t think that the name is mathematically inaccurate. Mathematical concepts do not generally have an unambiguous representation as data types in a programming language. A list of ints can represent a mathematical set, vector, polynomial, etc.

It is inaccurate in the context of a programming language such as Python to say e.g. set when you mean list if referring to the actual datatypes. The word “powerset” refers to the mathematical concept and representing a powerset as an iterable of lists is fine.

7 Likes

You could use the immutable version of the objects. Anyway, your point is perfectly valid :-) I have to think more about it.

The dupes makes a lot of confusion, creating duplicate “sets” inside the “powerset”.

And it’s also more computationally expensive.

That IMO it’s a good alternative choice. But there’s the option 3: something completely different.

I respect your opinion, but IMO there could be a lot of ones:

subsequences (per docstring)
powerlist
powertuples (maybe more appropriate)
concat_and_conquer
not_exactly_powerset_but_almost

Gemini also suggested me:

combos
itertools_recipe_number_5
tuple_de_force

Not bad.

Seriously, I think subsequences and combos are valid alternatives.

Most people don’t care also about itertools :wink:

Well, so we should not use an inappropriate math name. But maybe practicality beats purity? I have to think about it.

That’s interesting. Why? Because it seems to me that they are perfectly the same, apart the fact that in Python ((1, 2), 3) != (1, 2, 3)

I just realized that there’s a doc section, and I moved the discussion here. Anyway, to be serious, Paul had convinced me with the unhashable and “not a real good alternative name” arguments, and I was convinced also by myself with the “hey, I’m calling frozenset for every subsequence!” argument. I just realized that in the middle of my post, so the hilarious tone was directed to me :-D.

For me the discussion is closed. Ty Paul :-). If you want to discuss about math vs py tuples, I’m intrigued.

1 Like

All I meant by my comment was that in the maths where I learned about tuples as part of set theory and mathematical logic, a tuple <x, y> is defined as the set {x, {x, y}} (see here). On the other hand, a Python tuple bears no relation to Python sets, in this or any other way.

3 Likes

Ok, so it’s the same conclusion I had when you mentioned it and I did a fast and furious search on Wikipedia, where it says that in math, this is True ((1, 2), 3) == (1, 2, 3).

So tuples in math can be represented as sets. That’s really intriguing. Don’t know why at my university never talked about tuples, but maybe it was because I didn’t take math as path?

It tends to be covered in foundational stuff (set theory and logic, as I mentioned) where the focus is very much on building everything from the bare minimum of concepts. In everyday maths, everyone knows what a tuple is and how it behaves, so there’s no point worrying about the theoretical fact that you can derive tuples from sets.

You can do the same with numbers - if you define the number 0 as the empty set, and the number n + 1 as the set containing all the numbers up to and including n, you can derive the natural numbers out of thin air, and prove their properties. It’s fun, but largely irrelevant for any practical purpose[1].


  1. Which could be considered the definition of “pure maths”, of course :slightly_smiling_face: ↩︎

2 Likes

This seems like a good place to put the Natural Number Game for anyone who hasn’t heard of the Lean programming language. :slight_smile:

2 Likes