-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdunder_methods.py
55 lines (41 loc) · 1.3 KB
/
dunder_methods.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# -*- coding: utf-8 -*-
"""
Created on Tue Aug 18 14:36:41 2020
@author: saura
"""
class ActionFig():
def __init__(self, color, age):
self.color = color
self.age = age
self.my_dict = {"name": "yoyo", "has_pets": "Dog"}
def __str__(self):
return f'{self.color}'
def __len__(self):
return 5
def __call__(self):
return 'hello...??'
def __getitem__(self, i):
return self.my_dict[i]
def __del__(self):
return 'deleted'
hulk = ActionFig("red", 0)
print(hulk.__str__())
print(hulk)
print(str(hulk))
print(str('Printing an string'))
print(hulk.__len__())
print(len(hulk))
print(len('0123456789'))
print(hulk.__call__())
print(hulk()) # with call method, we get this special power to call a method with curly braces
print(hulk.__getitem__("has_pets"))
print(hulk.__repr__()) # it is same as print(hulk), it prints the memory location of the object
print(hulk.__del__())
del hulk # this deletes the variable passed, but as we have modified it, it won't delete the instance now
a = 5
del a
# print(a)
'''
this will give us error, because we have deleted the variable a,
because we haven't modified the base class __del__ method, so its performing normal
'''