Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #28103

Re: class object's attribute is also the instance's attribute?

Path csiph.com!usenet.pasdenom.info!dedibox.gegeweb.org!gegeweb.eu!nntpfeed.proxad.net!proxad.net!feeder1-2.proxad.net!usenet-fr.net!nerim.net!novso.com!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <d@davea.name>
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; 'syntax': 0.03; 'value,': 0.03; 'interpreter': 0.04; 'output': 0.04; 'attribute': 0.05; 'class,': 0.07; 'attribute.': 0.09; 'fetch': 0.09; "object's": 0.09; 'cc:addr:python-list': 0.10; 'a()': 0.16; 'a(object):': 0.16; 'instances,': 0.16; 'subject:class': 0.16; 'subject:instance': 0.16; 'subject:object': 0.16; 'wrote:': 0.17; 'documented': 0.17; 'instance': 0.17; 'instance,': 0.17; 'cc:2**0': 0.23; 'this:': 0.23; 'cc:no real name:2**0': 0.24; 'cc:addr:python.org': 0.25; 'header:In-Reply-To:1': 0.25; 'header :User-Agent:1': 0.26; 'looks': 0.26; 'am,': 0.27; "doesn't": 0.28; 'behavior.': 0.29; 'class': 0.29; 'code': 0.31; 'print': 0.32; 'instances': 0.33; 'right?': 0.33; 'same.': 0.35; 'subject:?': 0.35; 'but': 0.36; 'useful': 0.36; 'one,': 0.37; 'subject:: ': 0.38; 'some': 0.38; 'received:192': 0.39; 'received:192.168': 0.40; 'your': 0.60; 'most': 0.61; 'first': 0.61; 'kind': 0.61; 'provide': 0.62; 'header:Reply-To:1': 0.68; 'received:74.208': 0.71; 'reply-to:no real name:2**0': 0.72; "'it": 0.84; 'received:74.208.4.194': 0.84
Date Thu, 30 Aug 2012 07:53:19 -0400
From Dave Angel <d@davea.name>
User-Agent Mozilla/5.0 (X11; Linux x86_64; rv:14.0) Gecko/20120714 Thunderbird/14.0
MIME-Version 1.0
To 陈伟 <chenwei.address@gmail.com>
Subject Re: class object's attribute is also the instance's attribute?
References <3830e549-cb6d-4bcf-af45-f7c83ad2b65e@googlegroups.com>
In-Reply-To <3830e549-cb6d-4bcf-af45-f7c83ad2b65e@googlegroups.com>
Content-Type text/plain; charset=UTF-8
Content-Transfer-Encoding 8bit
X-Provags-ID V02:K0:S7TwkhSNoUZ2Quyie6Ua0jJXbxvVYTMRNUGRM9+qB+D +u8mEweiPv4K3hcJIO4QaIbJXQHgc0VZLstapEGxHDDqpHZDbS 2ItsYdD8/ayK/2EMSPGZZNXHklNEeGB/LzLo/NSSTkJ3Dd7TQm uTU0RLyVdFFdLsutB9vJ8zsUwWXE5aKAGYMAJGbRMYGcC5ccxS zUOwLrBpxsmG6cJ7sS5TDyI+NQ3fpgqgjT7G4eQbQkXazObb8z 1Z/ufQB4pmq5vpxKyJVEtfG8nvwRQwF2VhRVqfMPlGvGyf67NR eprPGvU65UmGkHTjJTxig6ZmwPgkGci6WvjznXqkkBnLpeZVg= =
Cc python-list@python.org
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
Reply-To d@davea.name
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://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 <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.3963.1346327629.4697.python-list@python.org> (permalink)
Lines 28
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1346327629 news.xs4all.nl 6871 [2001:888:2000:d::a6]:48841
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:28103

Show key headers only | View raw


On 08/30/2012 06:55 AM, 陈伟 wrote:
> when i write code like this:
>
> class A(object):
>      
>     d = 'it is a doc.'
>     
>
> t = A()
>
> print t.__class__.d
> print t.d
>
> the output is same.
>
> so it means class object's attribute is also the instance's attribute. is it right? i can not understand it.

In your example, you have no instance attribute.  So when you use the
syntax to fetch one, the interpreter looks first at the instance,
doesn't find it, then looks in the class, and does.  That is documented
behavior.  Some people use it to provide a kind of default value for
instances, which can be useful if most instances need the same value,
but a few want to overrride it.

-- 

DaveA

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

class object's attribute is also the instance's attribute? 陈伟 <chenwei.address@gmail.com> - 2012-08-30 03:55 -0700
  Re: class object's attribute is also the instance's attribute? Dave Angel <d@davea.name> - 2012-08-30 07:53 -0400
    Re: class object's attribute is also the instance's attribute? 陈伟 <chenwei.address@gmail.com> - 2012-08-30 05:57 -0700
    Re: class object's attribute is also the instance's attribute? 陈伟 <chenwei.address@gmail.com> - 2012-08-30 05:57 -0700
  Re: class object's attribute is also the instance's attribute? Ulrich Eckhardt <ulrich.eckhardt@dominolaser.com> - 2012-08-30 13:22 +0200
  Re: class object's attribute is also the instance's attribute? Marco Nawijn <nawijn@gmail.com> - 2012-08-30 05:34 -0700
    Re: class object's attribute is also the instance's attribute? Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2012-08-30 13:52 +0100
    Re: class object's attribute is also the instance's attribute? Hans Mulder <hansmu@xs4all.nl> - 2012-08-30 15:25 +0200
      Re: class object's attribute is also the instance's attribute? Marco Nawijn <nawijn@gmail.com> - 2012-08-30 07:11 -0700
        Re: class object's attribute is also the instance's attribute? Dave Angel <d@davea.name> - 2012-08-30 10:30 -0400
          Re: class object's attribute is also the instance's attribute? Marco Nawijn <nawijn@gmail.com> - 2012-08-30 07:48 -0700
            Re: class object's attribute is also the instance's attribute? Dave Angel <d@davea.name> - 2012-08-30 11:18 -0400
          Re: class object's attribute is also the instance's attribute? Marco Nawijn <nawijn@gmail.com> - 2012-08-30 07:48 -0700
            Re: class object's attribute is also the instance's attribute? Hans Mulder <hansmu@xs4all.nl> - 2012-08-30 17:20 +0200
              Re: class object's attribute is also the instance's attribute? Ben Finney <ben+python@benfinney.id.au> - 2012-08-31 09:58 +1000

csiph-web