1)
Below python code,
>>> def f():
return
creates a function
type object which has __name__
attribute with value 'f'
which looks fine.
But,
2)
Below line of code,
>>> x = 1024
creates int
type object but does not have __name__
attribute.
3)
lambda
expressions in python also does not associate any unique name for a function
type object.
>>> square = lambda x: x * x
>>> square.__name__
'<lambda>'
>>> summation = lambda x: x + 2
>>> summation.__name__
'<lambda>'
>>>
For any program written using Functional paradigm, it is considered an environment with name-object bindings in that environment.
So, Being functional paradigm programming beginner, How do I understand the significance of __name__
attribute? Why do I see such inconsistency in second and third case above in not maintaining __name__
attribute of an object?
__name__
is utterly unrelated to "name-object bindings" - you have assigned e.g. thelambda
to the namesquare
, through which you can now access it. The__name__
attribute for functions isn't even necessarily useful, as the function could be subsequently referenced by other names (f2 = f
) and dereferenced from its original__name__
(f = None
). It's worth reading this article for more on Python names. – jonrsharpe Feb 19 at 10:21