Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

class python __call__

#FROM GEEKSFORGEEKS <3
class Example:
    def __init__(self):
        print("Instance Created")
      
    # Defining __call__ method
    def __call__(self):
        print("Instance is called via special method")
  
# Instance created
e = Example()
  
# __call__ method will be called
e() #prints "Instance is called via special method"
Comment

class __call__ method python

# __call__ special function
# __call__() special method allows class instances to behave like functions
# i.e. can be called like a function - this is useful for class decorators
class Product:
    def __init__(self):
        print('Instance created')
       
    def __call__(self, a=5, b=10):
        print(a * b)
        
ans = Product()
# Instance created
ans(10, 20)     # __call__ method will be called here
# 200
ans()
# 50
Comment

PREVIOUS NEXT
Code Example
Python :: python match case 
Python :: how to avoid inserting duplicate records in orm django 
Python :: what is best app for Python 
Python :: Python List clear() 
Python :: python if in list 
Python :: clear many to many django 
Python :: range in python 
Python :: explain the use of return keyword python 
Python :: deleting key from dictionary 
Python :: get parent of current directory python 
Python :: dictionary multiple values per key 
Python :: print in python 
Python :: Print statement with multiple variables 
Python :: generate coordinates python 
Python :: python named tuples 
Python :: how to find greatest number in python 
Python :: Function to plot as many bars as you wish 
Python :: encode url 
Python :: how to create a new dataframe in python 
Python :: matplotlib window size 
Python :: full_like numpy 
Python :: 3d data visualization python 
Python :: aws python sdk 
Python :: python decorator class method 
Python :: dataframe change index 
Python :: tf dataset 
Python :: python number type 
Python :: cast as float python 
Python :: multivaluedictkeyerror django 
Python :: django for beginners 
ADD CONTENT
Topic
Content
Source link
Name
2+7 =