Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Practical use of tuple as a singleton

Tags:

python

tuples

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?

like image 353
SuperBiasedMan Avatar asked Aug 10 '15 08:08

SuperBiasedMan


People also ask

Is it possible to use a single item tuple as Singleton?

In the documentation on data models it mentions the possibility to use a single item tuple as a singleton, due to it's immutability. A tuple of one item (a ‘singleton’) can be formed by affixing a comma to an expression...

What is the difference between item1 and T1 in a tuple?

Here, item1 is the value of the tuple element and T1 is the type of the element stored in the tuple. Return Type: This method returns the components of the singleton tuple whose value is item1.

Can a tuple have more than one element?

But it can be used to create a tuple with from one to eight elements in it. This goes from a singleton (a new 1-tuple) to an “octuplet” (or an 8-tuple). It is possible to create a tuple with more than 8 elements, but that requires using the Rest property to nest tuple objects.

How to create a 1-tuple in Python?

You can create a 1-tuple using two different ways: You can create a singleton tuple using Tuple <T1> (T1) constructor. It initializes a new instance of the Tuple<T1> class. But when you create a tuple using this constructor then you have to specify the type of the element stored in the tuple. Here, item1 is the value of the tuple’s only component.


1 Answers

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).

like image 136
RemcoGerlich Avatar answered Oct 17 '22 01:10

RemcoGerlich