object()
object()
Return a new featureless object. It has the methods that are common to all instances of Python classes
x=object()
print(type(x)) # <class 'object'>
Object as function
We can't call object as a function() like this example.
class student_kit():
college_name='ABC College' #class attributes
#instance attributes
def __init__(self,name,section):
self.name= name
self.section=section
self.height=5 #instance attribute
Alex=student_kit('Alex','A') # object declaration
print(Alex.name,Alex.section,Alex.height)
Alex()
The last line will generate error like this.
TypeError: 'student_kit' object is not callable
Let us add __call__(self) to this.
class student_kit():
college_name='ABC College' #class attributes
#instance attributes
def __init__(self,name,section):
self.name= name
self.section=section
self.height=5 #instance attribute
def __call__(self):
print('Hi , you called me')
Alex=student_kit('Alex','A') # object declaration
print(Alex.name,Alex.section,Alex.height)
Alex()
Output
Alex A 5
Hi , you called me
Here we have added __call__(self) so we can use Alex().
So calling object as function ( with parenthesis () ) mean execution ojbects __call_(self) method.
«All Built in Functions in Python
« max()
Bitwise operators using format() »
int()
float()
← Subscribe to our YouTube Channel here
This article is written by plus2net.com team.
https://www.plus2net.com
plus2net.com