Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: jmp Newsgroups: comp.lang.python Subject: Re: class attribute Date: Thu, 28 Jan 2016 14:56:37 +0100 Lines: 70 Message-ID: References: <56aa1474$0$27833$426a74cc@news.free.fr> Mime-Version: 1.0 Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 8bit X-Trace: news.uni-berlin.de aDRgsQ4wrg0NgJa5d/dW6Qc/Xple6/IOaF16pvLpKO0w== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'ast': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'thx': 0.09; 'python': 0.10; 'def': 0.13; 'django': 0.13; '"les': 0.16; 'attributes,': 0.16; 'created.': 0.16; 'django.db': 0.16; 'doesnt': 0.16; 'manage.py': 0.16; 'metaclass': 0.16; 'received:80.91.229.3': 0.16; 'received:io': 0.16; 'received:plane.gmane.org': 0.16; 'received:psf.io': 0.16; 'subject:class': 0.16; 'wrote:': 0.16; 'attribute': 0.18; 'documented': 0.18; 'shell': 0.18; 'work,': 0.21; 'assign': 0.22; 'seems': 0.23; 'import': 0.24; 'header:In-Reply-To:1': 0.24; 'header:User-Agent:1': 0.26; 'header:X-Complaints-To:1': 0.26; 'skip:m 30': 0.27; 'sont': 0.29; 'guess': 0.31; 'probably': 0.31; 'skip:_ 10': 0.32; 'point': 0.33; 'class': 0.33; 'common': 0.33; 'int': 0.33; 'view,': 0.33; 'structure': 0.34; 'files,': 0.35; 'instance': 0.35; 'but': 0.36; 'too': 0.36; 'should': 0.36; 'framework': 0.36; 'to:addr:python-list': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'received:org': 0.37; 'names': 0.38; 'wrong': 0.38; 'why': 0.39; 'test': 0.39; 'to:addr:python.org': 0.40; 'details,': 0.60; 'received:194': 0.61; 'charset:windows-1252': 0.62; 'here': 0.66; 'article': 0.77; 'confusing': 0.84; 'examples.': 0.84 X-Injected-Via-Gmane: http://gmane.org/ X-Gmane-NNTP-Posting-Host: paris.sequans.com User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.6.0 In-Reply-To: <56aa1474$0$27833$426a74cc@news.free.fr> X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com comp.lang.python:102183 On 01/28/2016 02:15 PM, ast wrote: > 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 My guess is that models.Model has a metclass. Without going too much int details, the metaclass may change the class structure when it's created. django is very specific and very database oriented. " article = Article(titre="Bonjour", auteur="Maxime") article.contenu = "Les crêpes bretonnes sont trop bonnes !" " this is probably the wrong way to assign a value to 'contenu'. You should have a look at django help files, from what I remember it's very well documented with a lot of examples. jm