Are Python functions objects?

Are Python functions objects?

When you use the def statement or lambda expression, Python creates a function object for you.

We can assign attributes to function objects and retrieve them as follows.

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

We get the following output.

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

Yes, Python functions are full-fledged objects. They can have attributes and methods just like objects. Functions can have data variables, and you can even write functions inside them.

Leave a Reply

Your email address will not be published. Required fields are marked *