Follow

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

ยท ยท 3 ยท 0 ยท 1

@freemo It's the markdown format. I'm using ``` with newlines embedded in the OP, but the lines run together. Is there a place I can check the syntax?

@2ck It may be a CSS issue on my end actually. I'm not sure i tested code blocks. Do you want to look at the CSS and tell me if you see anything or you can leave that to me but might take me a bit longer to get to it (if you find a CSS fix i can add it right away though).

@freemo If you think it's upstream, then I'll file an issue on mastodon

@2ck no its likely something to do with our code, upstread doesnt even support markdown... just not sure how or why off hand

@kirschwipfel

QOTO is a fork of Mastodon and QOTO does. Vanilla mastodon would be able to render it by default but gargron went out of his way to remove that so it strips the formatting.

@2ck

@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

@2ck I do that sometimes.
It's kind of a historical fact now, I guess? At some point in py2 there wasn't "object" and then there was and you could opt-in to "new-style classes" with `class Foo(object):`. In py3 everything is new-style. IIRC ;)
#python

@meejah yeah, I mean specifically its use as a way to mint these sort-of do-nothing objects, not just its use as a type

@2ck I have certainly done that (and have seen it used).
There's also libraries like constantly.readthedocs.io/en/l for a similar use-case (but maybe .. a bit more "structured"?)
#python

@meejah that's kinda cool. If I ever used that though, I'd probably drop it the first chance I got in order to reduce dependencies. Would probably like it better as a code generation tool.

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.