Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python version of JavaScript ES6 Symbols

Is there a Python version of JavaScript's Symbol type? If not, what's the Pythonic way to declare a guaranteed-unique constant or property?

For example, in JavaScript, one could do:

const ALL_VALUES = Symbol()
const EVERY_OTHER_VALUE = Symbol()

function do_something_to_values(
    list_of_values, 
    values_to_affect
) {
    ...
}

And then one could call do_something_to_values(my_list, EVERY_OTHER_VALUE) and the function would check for equality to the EVERY_OTHER_VALUE constant.

How would one do this in Python?

like image 830
Ian Avatar asked Oct 21 '25 01:10

Ian


1 Answers

The keyword you’re looking for is sentinel. There’s been lots of discussion of this over the years. This is a good fairly recent solution

https://www.revsys.com/tidbits/sentinel-values-python/

like image 158
Luke Miles Avatar answered Oct 23 '25 16:10

Luke Miles