31
loading...
This website collects cookies to deliver better user experience
class Object:
def __init__(self):
print("Object innitialized")
x=Object()
__init__(self)
-This is the method that is called when you create an instance of an object. In this case it prints a stringx=Object()
-Here we are initializing x
with an instance of the objectclass Object:
def __init__(self,value):
self.x=5
def __repr__(self):
return f"Value is {self.x}"
x=Object(6)
print(x)
__repr__(self)
- It returns the string to be printed and doesn't take any argumentValue is 6
as result. If you want to get the string representation instead of printing it then you can use repr(x)
class Object:
def __len__(self):
return 4
x=Object()
print(len(x))
__len__(self)
-This method returns the length of the object and that's it. It must return an integer and should not take any argument. class Object:
def __len__(self):
return 4
x=Object()
print(len(x))
__len__(self)
-This returns the length of the object and that's it. It should always return a number and should never take any argument