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


Groups > comp.lang.python > #62158

Re: Type of an object: ‘obj.__class__’ versus ‘type(obj)’

X-Received by 10.180.92.226 with SMTP id cp2mr931429wib.6.1387264470144; Mon, 16 Dec 2013 23:14:30 -0800 (PST)
Path csiph.com!usenet.pasdenom.info!dedibox.gegeweb.org!gegeweb.eu!nntpfeed.proxad.net!proxad.net!feeder1-2.proxad.net!209.85.212.215.MISMATCH!w6no6838049wiw.0!news-out.google.com!h6ni9120wiy.1!nntp.google.com!feeder1.cambriumusenet.nl!82.197.223.108.MISMATCH!feeder2.cambriumusenet.nl!feed.tweaknews.nl!194.109.133.86.MISMATCH!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.007
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; 'attribute': 0.07; 'detect': 0.07; 'attributes': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'type,': 0.09; 'mostly': 0.14; 'finney': 0.16; 'ignoring': 0.16; 'nick': 0.16; "object's": 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'sense,': 0.16; 'subject:object': 0.16; 'subject:versus': 0.16; 'wrote:': 0.18; 'obviously': 0.18; '(the': 0.22; '(in': 0.22; 'header:User-Agent:1': 0.23; 'subjectcharset:iso-8859-7': 0.24; 'mon,': 0.24; 'skip:" 20': 0.27; 'header:X-Complaints-To:1': 0.27; 'dec': 0.30; 'list:': 0.30; "i'm": 0.30; '(which': 0.31; "d'aprano": 0.31; 'object.': 0.31; 'steven': 0.31; 'writes:': 0.31; '(e.g.': 0.33; 'entirely': 0.33; 'comment': 0.34; 'but': 0.35; 'there': 0.35; 'charset:us-ascii': 0.36; 'should': 0.36; 'skip:o 20': 0.38; 'sometimes': 0.38; 'ben': 0.38; 'question,': 0.38; 'to:addr:python-list': 0.38; 'rather': 0.38; 'sure': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'is.': 0.60; 'customized': 0.61; 'you.': 0.62; 'received:217': 0.63; 'real': 0.63; 'more': 0.64; 'great': 0.65; 'direct': 0.67; 'between': 0.67; 'wish': 0.70; 'hand': 0.80; 'python-dev': 0.84; 'subject::': 0.85; '2013': 0.98
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From dieter <dieter@handshake.de>
Subject Re: Type of an object: ‘obj.__class__’ versus ‘type(obj)’
Date Tue, 17 Dec 2013 08:14:09 +0100
References <mailman.4162.1387158693.18130.python-list@python.org> <52af7bfe$0$29976$c3e8da3$5496439d@news.astraweb.com>
Mime-Version 1.0
X-Gmane-NNTP-Posting-Host pd9e0966b.dip0.t-ipconnect.de
User-Agent Gnus/5.1008 (Gnus v5.10.8) XEmacs/21.4.22 (linux)
Cancel-Lock sha1:UAyoa+cT0PgaToG0zNlOkQ1I4NU=
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
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>
Newsgroups comp.lang.python
Message-ID <mailman.4265.1387264469.18130.python-list@python.org> (permalink)
Lines 28
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1387264469 news.xs4all.nl 2897 [2001:888:2000:d::a6]:51524
X-Complaints-To abuse@xs4all.nl
Content-Type text/plain; charset=us-ascii
Xref csiph.com comp.lang.python:62158

Show key headers only | View raw


Steven D'Aprano <steve+comp.lang.python@pearwood.info> writes:

> On Mon, 16 Dec 2013 12:51:21 +1100, Ben Finney wrote:
> ... "type(obj)" versus "obj.__class__"
> That is an excellent question, I only wish I had an excellent answer to 
> give you. Obviously great minds think alike because I was going to ask 
> the same question, prompted by this comment from Nick Coghlan on the 
> python-dev list:
>
> "...type(obj).__name__ (working with the concrete type, ignoring any
> proxying) or obj.__class__.__name__ (which takes proxying into 
> account)..."
>
> So there is a difference between them, but I'm not entirely sure what it 
> is.

I understand the difference: sometimes you work with proxies
(e.g. "weakref" proxies). A proxie should work mostly like the proxied
object - but in rare cases, you want to detect that what you have
is actually a proxie rather than the real object.
You can use "type(obj)" to check the real type ob "obj" (in some
sense, it is more direct - more reliable; giving you the real
type of "obj"). "obj.__class__" on the other hand uses standard
attribute access - and proxying may have customized attribute access
to access the proxied object's attributes rather than its own: then
"obj.__class__" would give you not the type of "obj" (the proxie)
but that of the proxied object.

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


Thread

Type of an object: ‘obj.__class__’ versus ‘type(obj)’ Ben Finney <ben+python@benfinney.id.au> - 2013-12-16 12:51 +1100
  Re: Type of an object: ‘obj.__class__’ versus ‘type(obj)’ Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-12-16 22:17 +0000
    Re: Type of an object: ‘obj.__class__’ versus ‘type(obj)’ dieter <dieter@handshake.de> - 2013-12-17 08:14 +0100
      Re: Type of an object: ‘obj.__class__’ versus ‘type(obj)’ Steven D'Aprano <steve@pearwood.info> - 2013-12-17 07:42 +0000
        Re: Type of an object: Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2013-12-17 23:35 +1300
          Re: Type of an object: Ethan Furman <ethan@stoneleaf.us> - 2013-12-17 06:50 -0800
          Re: Type of an object: Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-12-17 15:08 +0000
            Re: Type of an object: Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2013-12-18 11:15 +1300
              Re: Type of an object: Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-12-17 23:51 +0000
                Re: Type of an object: Ethan Furman <ethan@stoneleaf.us> - 2013-12-17 17:10 -0800
                Re: Type of an object: Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2013-12-19 00:45 +1300
                Re: Type of an object: Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2013-12-19 00:39 +1300

csiph-web