-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcallable_github.py
32 lines (26 loc) · 1.12 KB
/
callable_github.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
class Greeting:
def __call__(self, name):
# This class defines a custom callable object.
# Adding the __call__ method allows instances of this class
# to behave like functions when called.
return f"Hello, {name}! Welcome to the callable world!"
# Regular function
def add(a, b):
# A standard function in Python is always callable.
return a + b
# Normal object
class NonCallable:
# This class does NOT implement the __call__ method.
# Instances of this class cannot behave like functions.
pass
if __name__ == "__main__":
# Create instances
greeting = Greeting()
non_callable = NonCallable()
# Test callable objects
print("Is 'greeting' callable?", callable(greeting)) # True, since __call__ is defined
print("Is 'add' callable?", callable(add)) # True, since it's a function
print("Is 'non_callable' callable?", callable(non_callable)) # False, it's a regular object
# Use the callable object
if callable(greeting):
print(greeting("Betül")) # "Hello, Betül! Welcome to the callable world!"