Computer >> Computer tutorials >  >> Programming >> Python

Are Python functions objects?


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.