Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are there any uses of the empty tuple in Python?

Tags:

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.

like image 675
user237419 Avatar asked Jan 28 '11 12:01

user237419


2 Answers

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 ) ) 
like image 89
S.Lott Avatar answered Sep 19 '22 15:09

S.Lott


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.

like image 25
Marcelo Cantos Avatar answered Sep 17 '22 15:09

Marcelo Cantos