6

In the documentation on data models it mentions the possibility to use a single item tuple as a singleton, due to it's immutability.

Tuples

A tuple of one item (a ‘singleton’) can be formed by affixing a comma to an expression...

As far as I understand it in Python, a singleton functions similarly to a constant. It is a fixed value that maintains the same memory address so that you can test either equality or identity. For instance, None, True and False are all builtin singletons.

However using a tuple defined this way seems impractical for awkward syntax, considering this kind of usage:

HELLO_WORLD = ("Hello world",)
print HELLO_WORLD
> ('Hello world',)
print HELLO_WORLD[0]
> Hello world

Not to mention, it only functions as a singleton if you remember to index it.

HELLO_WORLD[0] = "Goodbye world"

Traceback (most recent call last):
  File "<pyshell#3>", line 1, in <module>
    HELLO_WORLD[0] = "Goodbye world"
TypeError: 'str' object does not support item assignment

Meaning that you could easily do this:

HELLO_WORLD = "Goodbye world"
print HELLO_WORLD
> Goodbye world

Given these limitations, is there any point to this singleton implementation? The only advantage I see is that it's simple to create. Other approaches I've seen have been more complicated approaches (using classes etc.) but is there some other use for this I'm not thinking of?

Community
  • 1
  • 1
SuperBiasedMan
  • 9,814
  • 10
  • 45
  • 73
  • What do you want it to do instead? Give an example from some other language if relevant. – Paul Aug 10 '15 at 09:03
  • 5
    Single item tuple _is_ a singleton (i.e. by definition), so I don's seem to understand "possibility to use a single item tuple as a singleton". If you're referring to the design pattern of the same name, it's unrelated. – bereal Aug 10 '15 at 09:03
  • 2
    are you sure "singleton" in this context means the same as in software design? i have thought this is just the name for a tuple with exactly one element. – hiro protagonist Aug 10 '15 at 09:04
  • 1
    This question is off-topic because it is caused by OP misunderstanding the use of the word "singleton" in the python documentation. – l4mpi Aug 10 '15 at 09:09
  • So, instead of voting off-topic, why not take the time to explain what the word singleton means in this particular excerpt of the Python documentation? – YuppieNetworking Aug 10 '15 at 09:13
  • @YuppieNetworking you mean like bereal already did in their comment? TL;DR it does not mean "you can use this to implement the singleton pattern" but refers to a container holding a single item; the same as e.g. [singletonList in Java](https://stackoverflow.com/questions/4801794/use-of-javas-collections-singletonlist). – l4mpi Aug 10 '15 at 09:34
  • 1
    This question is on-topic because it clarifies a potential misunderstanding caused by the usage of the word "singleton" in the official Python documentation. ;-) – PM 2Ring Aug 10 '15 at 11:02

1 Answers1

9

I don't think this is useful for implementing singletons at all, it doesn't add anything: the tuple can still be overwritten by a new single-element tuple. Anyway, in your example your value is a string, which is already immutable itself.

But I think the line of documentation you refer to ("A tuple of one item (a ‘singleton’)"), doesn't refer to the singleton pattern at all, but rather to the mathematical usage of the word, see Wikipedia on Singleton (mathematics)

The term is also used for a 1-tuple (a sequence with one element).

RemcoGerlich
  • 30,470
  • 6
  • 61
  • 79