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.
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 ()
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:
Previous article: Function bytes in Python
Next article: Function chr () in Python