Does everyone use 's `object()` for unique tokens? Like:

```
no_match = object()

val = some_dict.get('key', no_match)

if val is not no_match:

do_something()
```

I don't know what else `object()` would be used for (and why they'd keep it).

@2ck
In Python, 'object' is the common base class of all classes.

Anyhow, I would use either:
if 'key' in some_dict: …
if only existence is of interest
or
class no_match: pass
if value will be actually used (no need to instantiate an object) anyhow, rethinking: for the later, your way might be more efficient, I need to time it

@kirschwipfel i use the pattern in the OP for mappings where the cost of lookup is significant (e.g., ZODB BTree), and where the standard "not found" value of None is a legitimate value to store, and I need to know it was stored. it's come up in a couple situations

@2ck
I timed the code now: your approach "object()" is faster than mine "class xxx: pass". So I'm going to change my habits :-)

Thanks for sharing

Sign in to participate in the conversation
CleverLibre Social

CleverLibre Social is an inclusive social instance for open discussion, learning, and community.
All cultures welcome.
Hate speech and harassment strictly forbidden.