-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcallable.py
38 lines (28 loc) · 1016 Bytes
/
callable.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
"""
The dunder method __call__ makes a class acting like a function.
Course: Python objects with special methods - Create better classes!.
Section: 4 Object Creation.
Chapter: 15. Callable.
Dunder methods:
__call__ -> Enables a class act like a function.
"""
#Old way to use it
class UserImageFactory:
def __init__(self, database_connection):
self.database_connection = database_connection
def create_user_image(self, user_name):
print("Access database", self.database_connection)
return f"Image object for {user_name}"
f = UserImageFactory("sqlite")
image = f.create_user_image("Vera")
print(image)
#using __call__
class UserImageFactory2:
def __init__(self, database_connection):
self.database_connection = database_connection
def __call__(self, user_name):
print("Access database", self.database_connection)
return f"Image object for {user_name}"
d = UserImageFactory2("Oracle")
image2 = d("Vera")
print(image2)