Are Python functions objects?

PythonServer Side ProgrammingProgramming

Python creates function objects for you when you use a def statement, or  when you use a lambda expression:

We can assign attributes to the function object and retrieve them as follows

Example

def foo(): pass
foo.score = 20
print(type(foo))
print(foo.score)
print(type(lambda x:x))

Output

We get the following output

C:/Users/TutorialsPoint1/~.py
<type 'function'>
20
<type 'function'>

Yes, python functions are full objects. They can have attributes and methods like objects. The functions can have data variables and even functions written inside of them. 

raja
Published on 07-Dec-2017 10:36:56
Advertisements