Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Python Tutorial
  • Interview Questions
  • Python Quiz
  • Python Glossary
  • Python Projects
  • Practice Python
  • Data Science With Python
  • Python Web Dev
  • DSA with Python
  • Python OOPs
Open In App
Next Article:
Assign Function to a Variable in Python
Next article icon

How to detect whether a Python variable is a function?

Last Updated : 26 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

There are times when we would like to check whether a Python variable is a function or not. This may not seem that much useful when the code is of thousand lines and you are not the writer of it one may easily stuck with the question of whether a variable is a function or not. We will be using the below methods to check the same.

  • By calling the built-in function callable()
  • By importing isfunction() from the inspect module
  • By using the type() function
  • By calling the built-in function hasattr() function
  • By using the isinstance() function

Detect whether a Python variable is a function or not by using a callable() function

It is a function that returns the boolean value True if the function is callable otherwise it returns false. And Syntax of the callable function.

Python3

def subtract(a, b):
    return a-b
 
print(callable(subtract))
value = 15
print(callable(value))
                      
                       

Output:

True False

Detect whether a Python variable is a function or not by using the inspect module 

Inspect is a module from which we will have to use the isfunction which returns a boolean value True if it is a function else return false.  And for using this, First, you have to import isfunction from inspect, and after that use, isfunction to get the boolean value.

Python3

from inspect import isfunction
 
def subtract(a, b):
    return a-b
 
print(isfunction(subtract))
val = 10
print(isfunction(val))
                      
                       

Output:

True False

Detect whether a Python variable is a function or not by using the type() function

It is a function that tells us the type of an object by which we will check if the type of object is a function then it is callable otherwise it is not callable.

Python3

def subtract(a, b):
    return a-b
 
print(type(subtract))
value = 'GFG'
print(type(value))
                      
                       

Output:

<class 'function'> <class 'str'>

Detect whether a Python variable is a function or not by using the hasattr() function

hasattr() is a function that tells us the type of an object by which we will check if the type of object is a function or not. It returns a boolean value as well just like callable().

Python3

def subtract(a, b):
    return a-b
 
 
print(hasattr(subtract, '__call__'))
value = 'GFG'
print(hasattr(value, '__call__'))
                      
                       

Output:

True False

 

isinstance() is a function that tells us the type of object by which we will check if the type of object is a function or not. It returns a boolean value as well just like hasattr().

Python3

import types
def subtract(a, b):
    return a-b
 
print(isinstance(subtract, types.FunctionType))
value = 'GFG'
print(isinstance(value, types.FunctionType))
                      
                       

Output:

True False

Detect whether a Python variable is a function or not by using inspect module:

One alternative approach to detect whether a Python variable is a function is to use the inspect module. This can be done by importing the isfunction method from inspect and then calling isfunction on the variable in question. For example:

Python3

import inspect
 
def test_function():
    pass
 
print(inspect.isfunction(test_function))  # Output: True
 
value = 10
print(inspect.isfunction(value))  # Output: False
                      
                       

Output:

True False


Next Article
Assign Function to a Variable in Python

P

princekumaras
Improve
Article Tags :
  • Python
Practice Tags :
  • python

Similar Reads

  • How to Use a Variable from Another Function in Python
    Using a variable from another function is important for maintaining data consistency and code reusability. In this article, we will explore three different approaches to using a variable from another function in Python. Use a Variable from Another Function in PythonBelow are the possible approaches
    2 min read
  • Assign Function to a Variable in Python
    In Python, functions are first-class objects, meaning they can be assigned to variables, passed as arguments and returned from other functions. Assigning a function to a variable enables function calls using the variable name, enhancing reusability. Example: [GFGTABS] Python # defining a function de
    4 min read
  • How to use/access a Global Variable in a function - Python
    In Python, variables declared outside of functions are global variables, and they can be accessed inside a function by simply referring to the variable by its name. [GFGTABS] Python a = "Great" def fun(): # Accessing the global variable 'a' print("Python is " + a) fun() [
    3 min read
  • How to check if a Python variable exists?
    Checking if a Python variable exists means determining whether a variable has been defined or is available in the current scope. For example, if you try to access a variable that hasn't been assigned a value, Python will raise a NameError. Let’s explore different methods to efficiently check if a va
    4 min read
  • How to Define and Call a Function in Python
    In Python, defining and calling functions is simple and may greatly improve the readability and reusability of our code. In this article, we will explore How we can define and call a function. Example: [GFGTABS] Python # Defining a function def fun(): print("Welcome to GFG") # calling a fu
    3 min read
  • How to Recall a Function in Python
    In Python, functions are reusable blocks of code that we can call multiple times throughout a program. Sometimes, we might need to call a function again either within itself or after it has been previously executed. In this article, we'll explore different scenarios where we can "recall" a function
    4 min read
  • How to Call a C function in Python
    Have you ever came across the situation where you have to call C function using python? This article is going to help you on a very basic level and if you have not come across any situation like this, you enjoy knowing how it is possible.First, let's write one simple function using C and generate a
    2 min read
  • How to call a function in Python
    Python is an object-oriented language and it uses functions to reduce the repetition of the code. In this article, we will get to know what are parts, How to Create processes, and how to call them. In Python, there is a reserved keyword "def" which we use to define a function in Python, and after "d
    5 min read
  • How to Add Function in Python Dictionary
    Dictionaries in Python are strong, adaptable data structures that support key-value pair storage. Because of this property, dictionaries are a necessary tool for many kinds of programming jobs. Adding functions as values to dictionaries is an intriguing and sophisticated use case. This article looks
    5 min read
  • Test if a function throws an exception in Python
    The unittest unit testing framework is used to validate that the code performs as designed. To achieve this, unittest supports some important methods in an object-oriented way: test fixturetest casetest suitetest runner A deeper insight for the above terms can be gained from https://www.geeksforgeek
    4 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences