Below python code,

>>> def f():

creates a function type object which has __name__ attribute with value 'f' which looks fine.



Below line of code,

>>> x = 1024

creates int type object but does not have __name__ attribute.


lambda expressions in python also does not associate any unique name for a function type object.

>>> square = lambda x: x * x
>>> square.__name__
>>> summation = lambda x: x + 2
>>> summation.__name__

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
    Note that having an attribute __name__ is utterly unrelated to "name-object bindings" - you have assigned e.g. the lambda to the name square, 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 '15 at 10:21

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:

  1. Returning None,

  2. Returning an arbitrary string, such as <lambda>,

  3. 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?

| improve this answer | |
  • lambda expressions are used as part of functional paradigm programming. functional paradigm is considered as environment with (name,object) bindings as mentioned in the query. – overexchange Feb 19 '15 at 7:47
  • 2
    Python is not a functional language. – Benjamin Hodgson Feb 19 '15 at 8:14
  • 3
    As the Wiki article puts it (emphasis mine): "Python is a multi-paradigm programming language: object-oriented programming and structured programming are fully supported, and there are a number of language features which support functional programming and aspect-oriented programming (including by metaprogramming and by magic methods)... The design of Python offers only limited support for functional programming in the Lisp tradition." – jonrsharpe Feb 19 '15 at 10:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.