Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Practical Python Design Patterns

Practical Python Design Patterns

Any production application requires good use of design patterns and this talk will be about popular design patterns/choices and their implementation. The talk will also cover basics of metaclasses and be using metaclasses in some design choices practically.

Avatar for PyLondinium18

PyLondinium18

June 09, 2018
Tweet

More Decks by PyLondinium18

Other Decks in Programming

Transcript

  1. About me ▪ Co-Founder/Director of Zentropy Technologies – We do

    end to end implementations of Fin Tech and Data science projects ▪ Before finding Zentropy, had a working experience of 10 years in a leading Hedge fund ▪ Part of the organizing team of PyCon India, 2018
  2. Disclaimer J ▪ This presentation is based on Python 3

    and later ▪ Almost all of this works in Python 2 as well – However, not really tested by Presenter ▪ Tit-bits gained over a period by working in Python.
  3. Classes in Python ▪ First class supported entities ▪ Technically

    class is an instance of type ▪ Sugar over the native ‘type’ in Python
  4. type.__doc__ Init signature: type(self, /, *args, **kwargs) Docstring: type(object_or_name, bases,

    dict) Usages: 1. type(object) -> the object's type 2. type(name, bases, dict) -> a new type Type: type
  5. Create a class using a type type(name, bases, dict) ▪

    name – Name of the Class ▪ bases – Base classes of the class ▪ dict – The initial set of attributes and methods
  6. Quick Recap ▪ Class is an instance of ’type’ –

    the holy grail ▪ Type can be used to create dynamic classes ▪ Life cycle of a class creation – It applies for the Class creation (instance of type).
  7. Meta Classes ▪ A subclass of ‘type’ ▪ Change the

    way the class behaves ▪ Commonly called ‘Black magic’ in Python – Use it only when actually needed
  8. The gory details of syntax ▪ Creating a Meta class

    class MyMetaClass(type): pass ▪ Using a Meta class class MyClass(metaclass=MyMetaClass): pass
  9. Lifecycle of Meta class based class __new__() __init__() __call__() Meta

    class __new__() __init__() __call__() Actual class __new__() __init__() Actual class creation __call__() __new__() __init__() Class instance creation
  10. The broad idea ▪ Class Behavior changes – – Use

    __new__() and __init__() of meta class ▪ Change Behavior of instance of the class (typical object) – Use __call__() of meta class
  11. References ▪ Python 3 Meta Programming by David Beazley -

    https://www.youtube.com/watch?v=sPiWg5jSoZI ▪ Practical Metaclasses and Descriptors by Sim Zacks - - https://www.youtube.com/watch?v=Nay7lRPwnC4 ▪ Stack Overflow
  12. PyCon India, 2018 ▪ October 5-9, 2018 @ Hyderabad, India

    ▪ Tenth Anniversary ▪ CFP and Tickets open ▪ Keynote speakers – Carol Willing – Travis Oliphant – Armin Ronacher ▪ More details @ in.pycon.org