-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathpolymorphism-2.py
32 lines (25 loc) · 896 Bytes
/
polymorphism-2.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
#polymorphism-2.py
# Another example for Polymorphism are the several inbuilt
# functions in Python. Take for example, the builtin function
# called 'len'.
# 'len' is available for almost all types, such as strings,
# ints, floats, dictionaries, lists, tuples etc..
# When len is called on a type, it actually calls the inbuilts
# private function 'len' on that type or __len__
# Every object type that supports 'len' will have a private
# 'len' function inbuilt.
# Hence, for example, a list type already has a 'len()'
# function inbuilt in the Python code, and when you run the
# len() function on the data type, it checks if the len
# private function is available for that type or not.
# If it is available, it runs that.
text = ["Hello", "Hola", "helo"]
print("text",len(text))
print("hello",len("Hello"))
print("char",len({"a": 1, "b": 2, "c": 3}))
'''
O/P-
text 3
hello 5
char 3
'''