2. Meta Classes
ļ” A Design pattern
ļ” Dynamically create classes
ļ” Enables Flexibility,Good API
ļ” Used by Frameworks
ļ” Perform āMagicā
3. Where Meta Classes?
ļ” Frameworks, frameworks, frameworks.
ļ” Django uses them extensively.
ļ” Scrappy, a web scrapping framework.
ļ” Flexible Django apps. Eg: Django-registration
4.
5. Executes the code when encountered
Creates namespace and stores all variables in it
6. `type` is an inbuilt MetaClass
Dynamic class = Static classes
8. Meta Class Magic
Define a class extending object
Examined type is string, Magic!
Enables Declarative Syntax
9. MetaClasses, not always
ļ” Simple function decorators
ļ” Unintuitive to end developers, unless
explained
ļ” Python 2.6+ has Class Decorators
ļ” But being powerful, where they are needed,
indespesible
11. Django Models Creation
ļ” Declarative Syntax, No `__metaclass__`
ļ” New model created in place
ļ” Options into `Options` object
ļ” Register in registry called `AppCache`
ļ” Add attributes to new model
12. Inside Django Models
ļ” Return the same registered Model from
AppCache
ļ” Original options and Meta options stored in
`_meta`
ļ” Add more useful attributes/classes like
`DoesnotExist`, `module_name` and
`class_name`
14. ļ” Rendering in html
ļ” Validation for right data
ļ” Displaying errors
ļ” Customization options
ļ” Provided model, storing to the database
15. Dynamic Django forms - 1
Forms API is flexible, but often needs Meta Classes
`base_fields` and `fields` list store all fields
Change field attributes while initialization