Skip to content

Latest commit

 

History

History

state

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 

State Design Pattern

Videos

Section Video Links
State Overview State Overview State Overview State Overview
State Use Case State Use Case State Use Case State Use Case
__call__ Attribute Dunder __call__ Attribute Dunder __call__ Attribute Dunder __call__ Attribute

Book

Cover Links
Design Patterns In Python (ASIN : B08XLJ8Z2J)    https://www.amazon.com/dp/B08XLJ8Z2J
   https://www.amazon.co.uk/dp/B08XLJ8Z2J
   https://www.amazon.in/dp/B08Z282SBC
   https://www.amazon.de/dp/B08XLJ8Z2J
   https://www.amazon.fr/dp/B08XLJ8Z2J
   https://www.amazon.es/dp/B08XLJ8Z2J
   https://www.amazon.it/dp/B08XLJ8Z2J
   https://www.amazon.co.jp/dp/B08XLJ8Z2J
   https://www.amazon.ca/dp/B08XLJ8Z2J
   https://www.amazon.com.au/dp/B08Z282SBC

Overview

... Refer to Book or Design Patterns In Python website to read textual content.

Terminology

... Refer to Book or Design Patterns In Python website to read textual content.

State UML Diagram

State UML Diagram

Source Code

... Refer to Book or Design Patterns In Python website to read textual content.

Output

python.exe ./state/state_concept.py
I am ConcreteStateB
I am ConcreteStateA
I am ConcreteStateB
I am ConcreteStateA
I am ConcreteStateC

State Example Use Case

... Refer to Book or Design Patterns In Python website to read textual content.

State Example Use Case UML Diagram

State Example Use Case UML Diagram

Output

python.exe ./state/client.py
Task Started
Task Running
Task Finished
Task Started
Task Running

New Coding Concepts

Dunder __call__ Method

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

Summary

... Refer to Book or Design Patterns In Python website to read textual content.