-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCallables.py
executable file
·81 lines (62 loc) · 1.12 KB
/
Callables.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sun Apr 25 22:36:12 2021
@author: maherme
"""
#%%
a = callable(print)
print(a)
# All callables return a value:
result = print("hello")
print(result)
#%%
l = [1, 2, 3]
a = callable(l.append)
print(a)
result = l.append(4)
print(l)
print(result)
#%%
s = 'abc'
result = callable(s.upper) # Notice is s.upper and not s.upper()
print(result)
result = s.upper()
print(result)
#%%
from decimal import Decimal
result = callable(Decimal)
print(result)
a = Decimal('10.5')
print(type(a))
result = callable(a)
print(result)
#%%
class MyClass:
def __init__(self, x=0):
print('initializing...')
self.counter = x
result = callable(MyClass)
print(result)
a = MyClass(100)
a.counter
result = callable(a)
print(result)
#%%
class MyClass:
def __init__(self, x=0):
print('initializing...')
self.counter = x
def __call__(self, x=1):
print('updating counter...')
self.counter += x
b = MyClass()
MyClass.__call__(b, 10)
print(b.counter)
result = callable(b)
print(result)
b()
print(b.counter)
b(100)
print(b.counter)
#%%