Uncategorized

Why do python functions have __name__?


I’m confused by the attribute __name__ that python functions have by default. Let’s consider the following code:

def foo():
    return None

print(foo.__name__)

The code will print ‘foo’ as expected. A bit redundant, but why not I guess. However, if we add the following:

bar = foo
print(bar.__name__)

The above code also prints ‘foo’. From an objects and attributes perspective, this makes total sense. From a naming convention perspective, this is confusing.

Finally, the following code

baz = lambda:None
print(baz.__name__)

prints ”, which is hardly informative.

I feel like the __name__ attribute is trying to tell me something, but I can’t figure out what. Can I get some insight into the rationale behind this design choice?



Source link

Leave a Reply

Your email address will not be published. Required fields are marked *