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


Groups > comp.lang.python > #61986

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

Path csiph.com!usenet.pasdenom.info!news.etla.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!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.001
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'true,': 0.05; 'attribute': 0.07; '8bit%:30': 0.09; 'builtin': 0.09; 'constructor': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'python': 0.11; 'finney': 0.16; "object's": 0.16; 'pythonic': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'subject:object': 0.16; 'subject:versus': 0.16; 'wrote:': 0.18; 'all,': 0.19; 'header:User-Agent:1': 0.23; '"you': 0.24; 'received:comcast.net': 0.24; 'class.': 0.26; 'header:X -Complaints-To:1': 0.27; 'header:In-Reply-To:1': 0.27; 'them?': 0.31; 'probably': 0.32; 'possible.': 0.35; 'but': 0.35; 'there': 0.35; 'should': 0.36; 'ben': 0.38; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'simply': 0.61; 'choose': 0.64; 'skip:\xe2 10': 0.65; 'direct': 0.67; 'determine': 0.67; 'subjectcharset:utf-8': 0.72; 'subject::': 0.85
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Ned Batchelder <ned@nedbatchelder.com>
Subject Re: Type of an object: ‘obj.__class__’ versus ‘type(obj)’
Date Sun, 15 Dec 2013 21:50:23 -0500
References <7wk3f5k1gm.fsf@benfinney.id.au>
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding 8bit
X-Gmane-NNTP-Posting-Host c-50-133-228-126.hsd1.ma.comcast.net
User-Agent Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:24.0) Gecko/20100101 Thunderbird/24.2.0
In-Reply-To <7wk3f5k1gm.fsf@benfinney.id.au>
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.4166.1387162238.18130.python-list@python.org> (permalink)
Lines 27
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1387162238 news.xs4all.nl 2908 [2001:888:2000:d::a6]:36809
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:61986

Show key headers only | View raw


On 12/15/13 8:51 PM, Ben Finney wrote:
> Howdy all,
>
> What is the Pythonic way to determine the type of an object? Are there
> multiple valid ways, and when should each be used?
>
> We have ‘obj.__class__’, an attribute bound to the object's class. Or is
> it? When is that true, and when should we not rely on it?
>
> We have ‘type(obj)’, calling the constructor for the ‘type’ type in
> order to get a reference to the type of ‘obj’. Or is it? When is that
> true, and when should we not rely on it?
>
> Are there other ways to get at the type of a Python object? What reasons
> are there to choose or avoid them?
>

Generally, my answer would be, "You probably don't need the type as much 
as you think you do."   But when you do need it, type(x) is better than 
x.__class__, simply because we should always favor builtin functions 
over direct access of dunder-names where possible.

Also, don't overlook isinstance().

-- 
Ned Batchelder, http://nedbatchelder.com

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


Thread

Re: Type of an object: ‘obj.__class__’ versus ‘type(obj)’ Ned Batchelder <ned@nedbatchelder.com> - 2013-12-15 21:50 -0500

csiph-web