Python Classroom notes 11/Jul/2026

Design Patterns

  • In programming languages we have two terms
    • Patterns:
      • Solutions to known problems
      • This gives you the right way of implementation for an existing problem which is already solved by someone
    • AntiPatterns
      • Never follow this
  • Design Patterns: Refer Here
  • Design Patterns are classified into
    • Creational Patterns
    • Structural Patterns
    • Behavioral Patterns

Problem

  • I have a class which will get the current us dollar to indian rupee conversion
  • This class has a method convert which converts the currency according to current market
  • This is used in many places in my application.
  • Every where we use we create an object and then call convert
  • If an object needs to be created only once we use singleton pattern

Solution – Singleton

Python object creation process

  • Non clean singleton
class CurrencyConvertor:
    instance = None

    def __new__(cls):
        print("1. __new__ called")
        if cls.instance is not None:
            return CurrencyConvertor.instance
        cls.instance = super().__new__(cls)
        return cls.instance

    def __init__(self):
        print("inside init")

    def convert(self, value, source_currency, destination_currency):
        pass


if __name__ == '__main__':
    c1 = CurrencyConvertor()
    c2 = CurrencyConvertor()
    print(c1 == c2)

By continuous learner

enthusiastic technology learner

Leave a Reply

Discover more from Direct AI Powered By Quality Thought

Subscribe now to keep reading and get access to the full archive.

Continue reading