Section | Video Links |
---|---|
State Overview | |
State Use Case | |
__call__ Attribute |
... Refer to Book or Design Patterns In Python website to read textual content.
... Refer to Book or Design Patterns In Python website to read textual content.
... Refer to Book or Design Patterns In Python website to read textual content.
python.exe ./state/state_concept.py
I am ConcreteStateB
I am ConcreteStateA
I am ConcreteStateB
I am ConcreteStateA
I am ConcreteStateC
... Refer to Book or Design Patterns In Python website to read textual content.
python.exe ./state/client.py
Task Started
Task Running
Task Finished
Task Started
Task Running
Overloading the __call__
method makes an instance of a class callable like a function when by default it isn't. You need to call a method within the class directly.
class ExampleClass:
@staticmethod
def do_this_by_default():
print("doing this")
EXAMPLE = ExampleClass()
EXAMPLE.do_this_by_default() # needs to be explicitly called to execute
If you want a default method in your class, you can point to it using by the __call__
method.
class ExampleClass:
@staticmethod
def do_this_by_default():
print("doing this")
__call__ = do_this_by_default
EXAMPLE = ExampleClass()
EXAMPLE() # function now gets called by default
... Refer to Book or Design Patterns In Python website to read textual content.