Callable () function in Python

Continuing with the topic of Python built-in functions, the article will introduce you to the callable function () with syntax, usage as well as specific examples. Invites you to read the track.

callable () in Python checks whether objects can be called. If the object is allowed to call, the function returns TRUE, otherwise it returns FALSE.

Callable () function syntax in Python

 callable (object) 

Callable function parameters ():

The callable () function has only one argument, object.

What value does the callable () function return?

  1. TRUE: The object can be called.
  2. FALSE: The object cannot be called.

However, you should note that, even if callable () returns TRUE , there is a case of calling the failed object.

But if the result returned is FALSE , it will definitely not be able to call the object.

How does example 1: callable () work?

 x = 5 
print (callable (x))

def ktraHam ():
print ("Ktra")

y = kdHam
print (callable (y))

When you run the program, the output output will be:

 False 
True

In this example, object x cannot be called, and object y can be called.

Example 2: The object can be called

 class Quantrimang: 
def __call __ (self):
print ('Print screen image')

print (callable (Quantrimang))

When you run the program, the output output will be:

 True 

So the class object here can be called.

You can continue to perform other necessary commands.

 class Quantrimang: 
def __call __ (self):
print ('Print to screen')

InstanceOfQuantrimang = Quantrimang ()

# Prints 'Print to screen'
InstanceOfQuantrimang ()

Example 3: The object appears but cannot be called.

 class Quantrimang: 
def printLine (self):
print ('Print to screen')

print (callable (Quantrimang))

When you run the program, the output output will be:

 True 

The object in class Quantrimang here returns a call, but when executing another command an error occurs, for example:

 class Quantrimang: 
def printLine (self):
print ('Print to screen')

print (callable (Quantrimang))

InstanceOfQuantrimang = Quantrimang ()
# Error occurred
# 'Quantrimang' cannot be called
InstanceOfQuantrimang ()

Program reported an error:

 True 

Traceback (most recent call last):
File "", line 10, print
InstanceOfQuantrimang ()
TypeError: 'Quantrimang' object is not callable

Callable () has only a few notes like that. Remember to practice regularly with Python exercises.

See more:

  1. Bool () function in Python
  2. Ascii () function in Python
  3. Bin () function in Python

Previous article: Function bytes in Python

Next article: Function chr () in Python

4 ★ | 2 Vote

May be interested

  • The chr () function in PythonPhoto of The chr () function in Python
    in python, the chr () function returns a character (a string) from an integer that represents the unicode of the returned character.
  • Matrix in PythonPhoto of Matrix in Python
    in this article, we will learn about python matrices using nested lists and the numpy library.
  • Classmethod () function in PythonPhoto of Classmethod () function in Python
    in python, the classmethod () function returns a class method for the function. how is the syntax of classmethod () function, what parameters does it have and how to use it? invites you to read the track.
  • Working with File in PythonPhoto of Working with File in Python
    in this article, we will work with you to learn about file operations in python. more specifically, how to open files, read files, write to files, close files, and methods to work with files you should know. let's watch.
  • Manage files and folders in PythonPhoto of Manage files and folders in Python
    python also provides a variety of methods to handle various directory-related operations. in this article, we will learn about managing files and directories in python, namely creating folders, renaming folders, listing folders and working with them.
  • Error and Exception in PythonPhoto of Error and Exception in Python
    python often produces exceptions when an error occurs during execution. quantum will work with you to learn about the different exceptions built into this language.