Javascript symbols provide simple unique identifiers:
> const foo1 = Symbol('foo');
> foo1
Symbol(foo)
> const foo2 = Symbol('foo');
> foo1 === foo2
false
> let m = new Map();
> m.set(foo1, "bar");
> m.set(foo2, "rosco");
> m.get(foo1)
'bar'
> m.get(foo2)
'rosco'
This is useful for e.g. having unique return values from functions with special meanings when you can't use Exception
s.
I know in python you can use object()
or a nonce but neither of those render nice when you print them.
Is there anything like javascript's Symbol
available in python?