22

Is there any other purpose (besides being there because it needs to be) the empty tuple may have? Or: what would you use the empty tuple for? If anything. I just can't find an answer (well, a positive answer as in: "yes, there is"), please help me out with getting this question out of my head. "for testing if another tuple is empty" is not an acceptable answer since we should use 'not' operator for this.

jamylak
  • 128,818
  • 30
  • 231
  • 230
user237419
  • 8,829
  • 4
  • 31
  • 38

3 Answers3

21

Here's when.

def tuple_of_primes_less_than( n ):
    if n <= 2: return ()
    else:
        x, p = set( range(2,n) ), 2
        while p <= max(x):
            for k in range(2,int(2+math.sqrt(p))):
                x.discard(k*p)
            p += 1
        return tuple( sorted( x ) )
S.Lott
  • 384,516
  • 81
  • 508
  • 779
  • 1
    @adiru: wasn't that obvious **before** you asked the question? – S.Lott Jan 28 '11 at 12:46
  • 4
    clearly not. your question should be "why wasn't obvious?" I guess I got tricked by both its immutability and emptiness :) – user237419 Jan 28 '11 at 12:52
  • now that I act as a consumer of your function code, it's obvious. – user237419 Jan 28 '11 at 13:16
  • 3
    Bad example. Collections of objects of the same type should be lists. See, for example, [**this great answer**](http://stackoverflow.com/a/1708603/241039) (it's what I found now, but I remember seeing the same thoughts in a reputable source). – Oleh Prypin Sep 25 '12 at 16:39
  • I have to agree with @oleh-prypin. You want to have all primes, so if there are no primes, you return a list of size 0. – Johan Jun 07 '17 at 11:52
8

You might want to store the arguments to a function as a tuple, without knowing the structure of the function in advance. If it happens that the function you need to store the parameters for takes no parameters, you will need to store an empty tuple.

This is just one among many examples of why it's generally better not to question the existence of edge cases, just because we can't think of a use for them. The need to support the edge case invariably crops up before long.

Marcelo Cantos
  • 181,030
  • 38
  • 327
  • 365
  • 1
    I'm not questioning its existence, the empty tuple is mathematically correct. I'm just wondering how different people may have used it. – user237419 Jan 28 '11 at 12:22
  • 1
    "might want to store the arguments to a function as a tuple"? They **are** a tuple. `*args` **is** a tuple that may be empty. – S.Lott Jan 28 '11 at 12:28
  • @S.Lott: The point of interest is the fact that you might want to store them, not the fact that they are a tuple (which I already knew). But now that you mention it, the fact that function arguments are passed as a tuple is — on its own — a case of sometimes needing a zero-length tuple. – Marcelo Cantos Jan 28 '11 at 12:38
  • Marcelo is right. Think of an apply() with args parameter being required instead of optional. You'd use an empty tuple. – user237419 Jan 28 '11 at 12:40
  • This is not even an edge case. I use empty tuples quite a lot. – Lennart Regebro Jan 28 '11 at 12:45
  • @Marcelo: +1 for the example and for the advise. @Lennart: I made it an edge case because of how I built my question, although you must be right :) – user237419 Jan 28 '11 at 13:37
  • For the use case that you describe, why not use an empty list instead? Something to do with pickling those arguments, or somesuch? – eksortso Jan 28 '11 at 13:47
  • 1
    @Lennart: "Edge" doesn't imply infrequency. I just means that it's at the outer edge of some range. This kind of situation gets a special name because often such cases cause breakdowns in algorithms or the underlying mathematics; e.g., computing the average of a list of numbers is fine until the list is empty; thus, the *edge case* `len(L) == 0` causes problems when you want an average. – Marcelo Cantos Jan 28 '11 at 22:02
  • @eksorto: I wouldn't use a list because function parameters are passed as tuples to [`apply`](http://docs.python.org/library/functions.html#apply). Storing them as a list is unnecessary and inefficient. – Marcelo Cantos Jan 28 '11 at 22:04
4

Tuples represent arbitrary sequences of values. When you need to describe an empty sequence of values, you can use an empty tuple.

Gintautas Miliauskas
  • 7,744
  • 4
  • 32
  • 34
Ned Batchelder
  • 364,293
  • 75
  • 561
  • 662
  • 6
    @adirau: That's like asking when you need an empty list or an empty string. Or a string with the contents "Floprzy". :) – Lennart Regebro Jan 28 '11 at 12:46
  • 1
    probably, yes. or better, allow me to rephrase: "that's like asking when you need an empty immutable object" and leave the mutable ones outside of the question – user237419 Jan 28 '11 at 12:54