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

Follow

@kirschwipfel dope. thanks for letting me know ๐Ÿ˜

ยท ยท 0 ยท 0 ยท 0
Sign in to participate in the conversation
Qoto Mastodon

QOTO: Question Others to Teach Ourselves
An inclusive, Academic Freedom, instance
All cultures welcome.
Hate speech and harassment strictly forbidden.