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?
1
What inconsistency?
In the first case, you have a function. In the second case, you have a lambda expression, that is a function which has no name.
Python could have chosen one of those three approaches:
-
Returning
None
, -
Returning an arbitrary string, such as
<lambda>
, -
Raising an exception.
All three options are valid, given that the third one will result a try/except
instead of an if/else
. Python have chosen the second option. Why would you consider two others being more consistent?
3