Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #102184
| Path | csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail |
|---|---|
| From | Todd Dembrey <todd.dembrey@gmail.com> |
| Newsgroups | comp.lang.python |
| Subject | Re: class attribute |
| Date | Thu, 28 Jan 2016 14:13:19 +0000 |
| Lines | 77 |
| Message-ID | <mailman.55.1453990443.2338.python-list@python.org> (permalink) |
| 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 kSb4w/G2NdhwIfRFgH6eSw59cZwPIFKR4LNEaZKtTDMA== |
| Return-Path | <todd.dembrey@gmail.com> |
| 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; 'model,': 0.05; 'attributes': 0.07; 'classes.': 0.07; 'ast': 0.09; 'attribute.': 0.09; 'thx': 0.09; 'stored': 0.10; 'python': 0.10; 'def': 0.13; 'django': 0.13; '"les': 0.16; 'above)': 0.16; 'attributes,': 0.16; 'created.': 0.16; 'django.db': 0.16; 'doesnt': 0.16; 'example).': 0.16; 'manage.py': 0.16; 'metaclass': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'subject:class': 0.16; 'summary,': 0.16; 'wrote:': 0.16; 'attribute': 0.18; 'shell': 0.18; 'work,': 0.21; 'to:2**1': 0.21; 'defined': 0.23; 'seems': 0.23; 'import': 0.24; 'header:In-Reply-To:1': 0.24; 'header:User-Agent:1': 0.26; 'skip:m 30': 0.27; 'equivalent': 0.27; 'mostly': 0.27; 'object,': 0.27; 'reaches': 0.27; 'filed': 0.29; 'purposes.': 0.29; 'sont': 0.29; 'objects': 0.29; 'classes': 0.30; 'skip:_ 10': 0.32; 'run': 0.33; 'point': 0.33; 'class': 0.33; 'common': 0.33; 'view,': 0.33; 'message-id:@gmail.com': 0.34; 'skip:d 20': 0.34; 'received:google.com': 0.35; 'done': 0.35; 'instance': 0.35; 'received:74.125.82': 0.35; 'but': 0.36; 'should': 0.36; 'basic': 0.36; 'framework': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'names': 0.38; 'why': 0.39; 'test': 0.39; 'received:192': 0.39; 'to:addr:python.org': 0.40; 'your': 0.60; 'charset:windows-1252': 0.62; 'more': 0.63; 'different': 0.63; 'information': 0.63; 'within': 0.64; 'here': 0.66; 'below.': 0.66; 'worth': 0.67; 'article': 0.77; 'confusing': 0.84; 'todd': 0.84 |
| DKIM-Signature | v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=subject:to:references:from:message-id:date:user-agent:mime-version :in-reply-to:content-type:content-transfer-encoding; bh=bUirJuFhfNAeezKHmhH78HoW5N4pWxbSeIz/b/vzN3E=; b=n2OFo/Xtk8bE1aSXVNCnJQJUoCFWgnTjKsWg1Yt/0Lst8So2CQ5/7tqgFK/FsDdfLv h93lbSefpEUJkmMEAPui00YXRxXQLWB5a5DIAW3ibOr5RyfB3eqYz2E9d7FvW1QJXEjy jAQ8UqNsD7kotcce9ATqq+rhQwOL9dV/LaKtbKMao0WJWH6KNFdVnH02zC6fZXehXhzM iZGoJupSllcKEn+J/jqVTk6vtwAwfXNVfY/LkT+e/BGU+XS+fzxFT9P7mJbOIxc1BLAD pQeUrVXOskJtOacCQv3XCjGaKZs0IVzywVxRh+NPwSKF+1BZoM3wzmiBokzRW8fbn2Sk zyJA== |
| X-Google-DKIM-Signature | v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:subject:to:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-type :content-transfer-encoding; bh=bUirJuFhfNAeezKHmhH78HoW5N4pWxbSeIz/b/vzN3E=; b=PDbE+d7/GuEv6AVIQ2u86s8lMUtHFuDYRwAzpviwDMIq1ltn+3dFi/rmr1uTKj1hqP LhgjAbTrO4Q+0TWiKwZ+5OnYSzUSlMJWbS7zCfXx0GVAodqHH7jNtaItTqOj2HDjFjEG nBi2Pj0jTMd2rq9HatAex2/k3ACdpKNenQCnHe5+VU0+Tyn7in8bnjJzIx7YHIGoReCD XBuNZj+GV2YJ6vpHOD8NtyfLiXk3/ByGoABZLyaPqRGCLe8l6T9TzXJpY3H8mhgtiRa8 XYtSNm+7AuQm6uU0aCygxHzdTWNMm0KP0Ip6QI1lM/9PgRC0Z0sA7Kh87JucVJAKcX1+ PdIA== |
| X-Gm-Message-State | AG10YOT57AKQuMEosdw8/ZRzAx7jD47mnYgHw11TGIw8a2vXHlKcipC9KzOIm1Ja2VXoxw== |
| X-Received | by 10.194.93.199 with SMTP id cw7mr3917423wjb.64.1453990401729; Thu, 28 Jan 2016 06:13:21 -0800 (PST) |
| User-Agent | Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Thunderbird/38.5.1 |
| In-Reply-To | <56aa1474$0$27833$426a74cc@news.free.fr> |
| X-Mailman-Approved-At | Thu, 28 Jan 2016 09:14:03 -0500 |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.20+ |
| Precedence | list |
| List-Id | General discussion list for the Python programming language <python-list.python.org> |
| List-Unsubscribe | <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe> |
| List-Archive | <http://mail.python.org/pipermail/python-list/> |
| List-Post | <mailto:python-list@python.org> |
| List-Help | <mailto:python-list-request@python.org?subject=help> |
| List-Subscribe | <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Xref | csiph.com comp.lang.python:102184 |
Show key headers only | View raw
On 28/01/16 13:15, 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 Django Model classes are very different from a basic Python classes. It is worth having a look inside django/db/models/base.py for more information. But in summary, see below. The Model class has a metaclass of ModelBase which on __new__() returns a class constructed from the attributes which you have defined in your Model class (Article in this example). All the information is retained, it is just filed away neatly for other purposes. Have a look in the Article._meta attribute. You can find the fields you provided through Article._meta.fields. When you create Article (as above) you run the __init__() which reaches into the ._meta attribute of the class in order to create an object, which is then stored within the 'database'. This is done because you don't want to access the attributes on the Model, so they are removed when the Model is created. What you actually want to do is access them on the objects within the Model. Writing Article.objects.first().titre in Django Models is mostly equivalent to Article.titre from Python classes. Todd
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
class attribute "ast" <nomail@invalid.com> - 2016-01-28 14:15 +0100 Re: class attribute jmp <jeanmichel@sequans.com> - 2016-01-28 14:56 +0100 Re: class attribute Todd Dembrey <todd.dembrey@gmail.com> - 2016-01-28 14:13 +0000 Re: class attribute "ast" <nomail@invalid.com> - 2016-01-28 15:24 +0100 Re: class attribute Chris Angelico <rosuav@gmail.com> - 2016-01-29 01:56 +1100 Re: class attribute Joel Goldstick <joel.goldstick@gmail.com> - 2016-01-28 11:04 -0500
csiph-web