class attribute
ast
nomail at invalid.com
Thu Jan 28 08:15:26 EST 2016
hello
Here is a class from django framework
from django.db import models
class Article(models.Model):
titre = models.CharField(max_length=100)
auteur = models.CharField(max_length=42)
contenu = models.TextField(null=True)
date = models.DateTimeField(auto_now_add=True, auto_now=False,
verbose_name="Date de parution")
def __str__(self):
return self.titre
>From a Python point of view, what are titre, auteur, contenu and date ?
Are they class attributes, so common to all instance of Article ?
It seems so to me.
But if i do in a django shell (run with py manage.py shell)
>>> Article.titre
it doesnt work,
AttributeError: type object 'Article' has no attribute 'titre'
why ?
if I test on a small class
>>>class MyClass:
>>> i=0
>>>
>>> MyClass.i
>>> 0
works
When we create an object of class Article
article = Article(titre="Bonjour", auteur="Maxime")
article.contenu = "Les crêpes bretonnes sont trop bonnes !"
we use the same names titre, auteur, contenu, which should be instance
attribute this time. This is confusing to me
thx
More information about the Python-list
mailing list