DEV Community

chanduthedev
chanduthedev

Posted on

1 2

What is first class object in python?

In python, functions are first class objects in python.

Advantages of first class objects:

  • We can treat functions like other normal objects like int, string, list etc
  • We can pass functions as another function arguments, return a function in another function and assign a function to another variable
  • Define a function within a function (Inner functions)
  • This first class object plays a key role in python for decorator design pattern.

Examples:

# Simple function for adding two numbers
def add_num(num1, num2):
    return num1 + num2

# Passing function as a first argument
def add_values(fun, arg1, arg2):
    result = fun(arg1, arg2)
    return result
Enter fullscreen mode Exit fullscreen mode
  • Assigning function name to the variable:
fun = add_num
Enter fullscreen mode Exit fullscreen mode
  • Passing function as a parameter
two_num_sum = add_values(fun, 5, 8))
Enter fullscreen mode Exit fullscreen mode

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

Top comments (0)

Image of Docusign

🛠️ Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more