Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
Are Python functions objects?
Yes, Python functions are full objects. Python creates function objects when you use a def statement or a lambda expression. Like any other object, functions can be assigned to variables, passed as arguments, returned from other functions, and even have custom attributes.
Functions Have a Type and Support Attributes
Since functions are objects, they have a type (function) and you can assign custom attributes to them ?
Example
def foo():
pass
foo.score = 20
print(type(foo))
print(foo.score)
print(type(lambda x: x))
The output of the above code is ?
<class 'function'> 20 <class 'function'>
Assigning Functions to Variables
We can assign a function to a variable just like we can assign an integer, string, or list to a variable ?
Example
def greet(name):
print("Hello, " + name + "!")
hello = greet
hello("John")
The output of the above code is ?
Hello, John!
Here, we assign the function greet to a variable called hello and call it using that variable. Both greet and hello now reference the same function object.
Passing Functions as Arguments
We can pass a function as an argument to another function ?
Example
def add(x, y):
return x + y
def multiply(x, y):
return x * y
def apply(func, x, y):
return func(x, y)
result = apply(add, 2, 3)
print(result)
The output of the above code is ?
5
Storing Functions in Data Structures
Functions can be stored as values in data structures like dictionaries ?
Example
def say_hello():
return "Hello!"
def say_goodbye():
return "Goodbye!"
messages = {
"hello": say_hello,
"goodbye": say_goodbye
}
print(messages["hello"]())
print(messages["goodbye"]())
The output of the above code is ?
Hello! Goodbye!
Nested Functions (Closures)
Functions can be defined inside other functions and returned as values. This pattern is commonly known as a closure ?
Example
def outer_function():
def inner_function(x, y):
return x * y
return inner_function
product_func = outer_function()
result = product_func(3, 5)
print(result)
The output of the above code is ?
15
Applying a Function to a List
Functions can be passed to other functions that iterate over collections, similar to the built-in map() function ?
Example
def apply_to_list(func, lst):
result = []
for element in lst:
result.append(func(element))
return result
def square(x):
return x ** 2
numbers = [1, 2, 3, 4, 5]
squared_numbers = apply_to_list(square, numbers)
print(squared_numbers)
The output of the above code is ?
[1, 4, 9, 16, 25]
Setting Custom Attributes on Functions
Since functions are objects, you can set and read custom attributes on them ?
Example
def add(x, y):
return x + y
add.name = "Addition"
print(add(2, 3))
print(add.name)
The output of the above code is ?
5 Addition
Conclusion
Python functions are first-class objects. They can be assigned to variables, passed as arguments, returned from other functions, stored in data structures, and given custom attributes − making them extremely flexible and a core part of Python's design.
