For debugging purposes I wanted some lambda functions to be named, i.e. see a __repr__
string. Possible to change a function's repr in python? provides some good answers to how this can be properly done, but I do wonder about the following, "quick and dirty" version (which I won't keep in production code!):
I simply changed the functions' __qualname__
. Is that safe to use or do I have to expect some strange side effects - and if so, why? I.e. what other functions might rely on __qualname__
?
The code looks somewhat like this:
def make_listener(consumer):
def listener(obj):
if _is_doc_of_interest(obj, consumer):
consumer.put(obj)
listener.__qualname__ = f'listener({consumer})'
return listener
And will produce a bunch of functions which will look like:
[make_listener(c) for c in [MyObject(1), SomethingElse(True, 42, 'foo')]
> [
> <function listener(MyObject(1))>,
> <function listener(SomethingElse(True, 42, 'foo'))>
> ]
Again, this is only meant for the purposes of debugging (as I generate a whole lot of listeners and like to identify them quickly), but now I'm curious if this actually produces any side effects or if I can keep using it for quick investigations