Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about CollectivesThe top answer is correct.
But readers may be coming here searching answers about similarly named inner classes. They are present in popular libraries, such asDjangoDjango andWTFormsWTForms.
As DavidWpointedpoints out in the comments beneath this answer,these arelibrary specificlibrary-specific features and are not to be confused with the advanced, unrelatedPython language feature with a similar name unrelated Python language feature with a similar name.
Rather, these are namespaces within classes' dicts. They are constructed using inner classes for sake of readability.In
In this example special field,abstract is visibly separate from fields of Author model.
from django.db import modelsclass Author(models.Model): name = models.CharField(max_length=50) email = models.EmailField() class Meta: abstract = True Another example is fromWTFormsthe documentation, forWTForms:
from wtforms.form import Formfrom wtforms.csrf.session import SessionCSRFfrom wtforms.fields import StringFieldclass MyBaseForm(Form): class Meta: csrf = True csrf_class = SessionCSRF name = StringField("name") This syntax does not get special treatment in the python programming language.Meta is not a keyword here, and does not trigger metaclass behavior. Rather, third-party library code in packages likeDjango andWTForms reads this property inconstructorthe constructors of certain classes, and elsewhere and.
The presence of these declarations modifiesit'sthe behavior in presence of the classes that have these declarations.WTFormsFor example,WTForms readsself.Meta.csrf to determine if the form needscsrfacsrf field.
The top answer is correct.
But readers may be coming here searching answers about similarly named inner classes. They are present in popular libraries, such asDjango andWTForms.
As DavidWpointed out,these arelibrary specific features and are not to be confused with advanced unrelated Python language feature with a similar name.
Rather, these are namespaces within classes' dicts. They are constructed using inner classes for sake of readability.In this example special field,abstract is visibly separate from fields of Author model.
class Author(models.Model): name = models.CharField(max_length=50) email = models.EmailField() class Meta: abstract = TrueAnother example is fromWTForms documentation,
from wtforms.csrf.session import SessionCSRFclass MyBaseForm(Form): class Meta: csrf = True csrf_class = SessionCSRF name = StringField("name") This syntax does not get special treatment in python programming language.Meta is not a keyword here and does not trigger metaclass behavior. Rather, library code reads this property inconstructor and elsewhere and modifiesit's behavior in presence of these declarations.WTForms readsself.Meta.csrf to determine if the form needscsrf field.
The top answer is correct.
But readers may be coming here searching answers about similarly named inner classes. They are present in popular libraries, such asDjango andWTForms.
As DavidWpoints out in the comments beneath this answer,these arelibrary-specific features and are not to be confused with the advanced, unrelatedPython language feature with a similar name.
Rather, these are namespaces within classes' dicts. They are constructed using inner classes for sake of readability.
In this example special field,abstract is visibly separate from fields of Author model.
from django.db import modelsclass Author(models.Model): name = models.CharField(max_length=50) email = models.EmailField() class Meta: abstract = True Another example is fromthe documentation forWTForms:
from wtforms.form import Formfrom wtforms.csrf.session import SessionCSRFfrom wtforms.fields import StringFieldclass MyBaseForm(Form): class Meta: csrf = True csrf_class = SessionCSRF name = StringField("name") This syntax does not get special treatment in the python programming language.Meta is not a keyword here, and does not trigger metaclass behavior. Rather, third-party library code in packages likeDjango andWTForms reads this property inthe constructors of certain classes, and elsewhere.
The presence of these declarations modifiesthe behavior of the classes that have these declarations.For example,WTForms readsself.Meta.csrf to determine if the form needsacsrf field.
MetaclassesThe top answer is correct.
But readers may be coming here searching answers about similarly named inner classes. They arenothing butpresent in popular libraries, such as Django and WTForms.
As DavidW pointed out,these are library specific features and are not to be confused with advanced unrelated Python language feature with asimplesimilar name.
Rather, these are namespaces within classes' dicts. They are constructed using innerclass of your model classclasses forexample:sake of readability.InDjango or pythonthis example special field,abstract is visibly separate from fields of Author model.
class Author(models.Model): name = models.CharField(max_length=50) email = models.EmailField() class Meta: abstract = TrueHereAnother example is from WTForms documentation, If abstract = True
from wtforms.csrf.session import SessionCSRFclass MyBaseForm(Form): class Meta: csrf = True csrf_class = SessionCSRF name = StringField("name") This syntax does not get special treatment in python programming language.Meta is not a keyword here and does not trigger metaclass behavior. Rather, library code reads thismodel will be an abstract base classproperty in constructor and elsewhere and modifies it's behavior in presence of these declarations.Metaclass will changeWTForms readsself.Meta.csrf to determine if thebehaviour of your base classform needs csrf field.
Metaclasses arenothing but asimple innerclass of your model class forexample:InDjango or python
class Author(models.Model): name = models.CharField(max_length=50) email = models.EmailField() class Meta: abstract = TrueHere, If abstract = True, thismodel will be an abstract base class.Metaclass will change thebehaviour of your base class.
The top answer is correct.
But readers may be coming here searching answers about similarly named inner classes. They arepresent in popular libraries, such as Django and WTForms.
As DavidW pointed out,these are library specific features and are not to be confused with advanced unrelated Python language feature with asimilar name.
Rather, these are namespaces within classes' dicts. They are constructed using innerclasses forsake of readability.Inthis example special field,abstract is visibly separate from fields of Author model.
class Author(models.Model): name = models.CharField(max_length=50) email = models.EmailField() class Meta: abstract = TrueAnother example is from WTForms documentation,
from wtforms.csrf.session import SessionCSRFclass MyBaseForm(Form): class Meta: csrf = True csrf_class = SessionCSRF name = StringField("name") This syntax does not get special treatment in python programming language.Meta is not a keyword here and does not trigger metaclass behavior. Rather, library code reads thisproperty in constructor and elsewhere and modifies it's behavior in presence of these declarations.WTForms readsself.Meta.csrf to determine if theform needs csrf field.
Metaclasses are nothing but a simple inner class of your model class for example:In Django or python
class Author(models.Model): name = models.CharField(max_length=50) email = models.EmailField() class Meta: abstract = TrueHere, If abstract = True, this model will be an abstract base class. Metaclass will change the behaviour of your base class.