The title
attribute is not data. It holds a model description only; an object describing what type of information the title
field should hold.
As such it is part of the class definition; individual instances of the Post
class will have a title
attribute that conforms to the constraints set in the models.CharField()
instance on the class.
You need to build such a model to describe to Django how to build form fields and how to build a SQL table for the Post
instances; both are concepts that need to have more type information than what Python normally itself needs.
Individual instances of Post
are given a title
attribute as well. That attribute then masks the class attribute:
p = Post(title='Some title')
print p.title # prints 'Some title'
Python looks at the instance directly first; if it does not have a title
attribute, lookup would then move to the class object. But that's not needed here, the Post.title
attribute is not found as the instance has a title
attribute itself.
In Python itself, there is no absolute distinction between 'data' and methods, by the way. Everything in Python is an object, including classes and methods. As such, looking up an attribute on an instance can find an object there too, including methods. If an attribute lookup there fails, then Python will look for the attribute on the class and base classes, and if that fails, lookup falls back to the metaclass even.
This is where mutable attributes come in; looking up ClassOne().v
fails on the instance, but succeeds on the class. Manipulating that list then alters ClassOne.v
the class attribute, and looking up v
on other instances once again will find the class attribute. This is how class attributes are shared, just like the methods on the class.