Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
Tutorialspoint

Are Python functions objects?

I have a python function like

def foo(x, y): 
      return x+y
foo(3,7)

How do I know that this function is an object?


1 Answer
Rajendra Dharmkar

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

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 objects. They can have attributes and methods like objects. The functions can have data variables and even functions written inside of them. 

Advertisements

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.