Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

The biggest difference between time and space is that you can't reuse time. -- Merrick Furst


devel / comp.lang.python / Re: Which more Pythonic - self.__class__ or type(self)?

SubjectAuthor
o Re: Which more Pythonic - self.__class__ or type(self)?Ethan Furman

1
Re: Which more Pythonic - self.__class__ or type(self)?

<mailman.2149.1677865861.20444.python-list@python.org>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=22170&group=comp.lang.python#22170

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: eth...@stoneleaf.us (Ethan Furman)
Newsgroups: comp.lang.python
Subject: Re: Which more Pythonic - self.__class__ or type(self)?
Date: Fri, 3 Mar 2023 09:50:40 -0800
Lines: 28
Message-ID: <mailman.2149.1677865861.20444.python-list@python.org>
References: <e3fa13c6-83fa-891e-ca6b-d3c80e940c21@gmail.com>
<7e9518b0-8e97-bcaf-24ba-e7f013690659@yahoo.co.uk>
<CAPTjJmqEeBWpo7BT_x96sBn86KWBxd6L2x2nY_hwS58B50KsTA@mail.gmail.com>
<ee805893-f945-0b9e-1768-6fccf60998ac@stoneleaf.us>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
X-Trace: news.uni-berlin.de LyCu8s67k9ZpMJHAjRlQkQtr+7xYZ9OV961b6b3w7lfA==
Return-Path: <ethan@stoneleaf.us>
X-Original-To: python-list@python.org
Delivered-To: python-list@mail.python.org
Authentication-Results: mail.python.org; dkim=pass
reason="2048-bit key; unprotected key"
header.d=stoneleaf.us header.i=@stoneleaf.us header.b=RD+kXj2W;
dkim-adsp=pass; dkim-atps=neutral
X-Spam-Status: OK 0.003
X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; '2023': 0.07; 'mar': 0.07;
'angelico': 0.09; 'methods,': 0.09; 'rarely': 0.09;
'received:217.70': 0.09; 'received:gandi.net': 0.09;
'received:mail.gandi.net': 0.09; 'situations': 0.09; 'skip:` 10':
0.09; 'though.': 0.09; 'talks': 0.14; '03:32,': 0.16; 'cases,':
0.16; 'from:addr:ethan': 0.16; 'from:addr:stoneleaf.us': 0.16;
'from:name:ethan furman': 0.16; 'gauld': 0.16; 'iirc,': 0.16;
'message-id:@stoneleaf.us': 0.16; 'option.': 0.16; 'pythonic':
0.16; 'subject:Pythonic': 0.16; 'tuple': 0.16; '~ethan~': 0.16;
'wrote:': 0.16; 'to:addr:python-list': 0.20; "i've": 0.22; 'fri,':
0.22; '(and': 0.25; 'anything': 0.25; 'seems': 0.26; 'classes':
0.26; 'object': 0.26; '>>>': 0.28; 'chris': 0.28; 'mostly': 0.28;
'header:User-Agent:1': 0.30; 'think': 0.32; 'but': 0.32;
'particular': 0.33; 'header:In-Reply-To:1': 0.34; 'those': 0.36;
'using': 0.37; 'use': 0.39; 'methods': 0.39; 'alan': 0.40;
'otherwise,': 0.40; 'true.': 0.40; 'skip:o 10': 0.61; "there's":
0.61; 'down': 0.64; 'subject:(': 0.64; 'generally': 0.67;
'outside': 0.67; 'received:217': 0.67; 'technical': 0.67;
'exactly': 0.68; 'functional': 0.69; 'subject:skip:s 10': 0.69;
'discuss': 0.78; 'difference.': 0.84; 'avoiding': 0.91;
'subject:type': 0.91; 'tend': 0.91; 'subject:more': 0.95
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=stoneleaf.us;
s=gm1; t=1677865843;
h=from:from:reply-to:subject:subject:date:date:message-id:message-id:
to:to:cc:mime-version:mime-version:content-type:content-type:
content-transfer-encoding:content-transfer-encoding:
in-reply-to:in-reply-to:references:references;
bh=a4s9gnYoTje40of/YAMbnMP6BTkF7WBaAdcMAD4AVRw=;
b=RD+kXj2WLsFu9ZJowkjddCpXJfjYgViHfeqXJhFE20WjMuP3z+NkOvSKb48y+u9+aFkv1W
D+wPanDaC3XWx3rhuYDhCRetbvTyJMSbhr0kxk/vCFmKyfW1hNZSlpSvdVPRvFEkedJItV
Zu94xew+ePb07qX9ZOuPhm2axVJrgHttbFTAwl0F5lvLZ5blkMABoLKeqAMymRHEyc64Iu
+gFji9I+NGp7AM2mz5XRzLjHD9UFncEyRvVqDX5uOIzgvJ5hEtDvu14y6CnSD/tKsqUQHY
FkYDRqmxwszVgxdk0y5YeoktxmR4XbMNEL3aUKkgLrR5cAQOnVV9K2GEAwquHg==
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101
Thunderbird/91.9.1
Content-Language: en-US
In-Reply-To: <CAPTjJmqEeBWpo7BT_x96sBn86KWBxd6L2x2nY_hwS58B50KsTA@mail.gmail.com>
X-BeenThere: python-list@python.org
X-Mailman-Version: 2.1.39
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: <https://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>
X-Mailman-Original-Message-ID: <ee805893-f945-0b9e-1768-6fccf60998ac@stoneleaf.us>
X-Mailman-Original-References: <e3fa13c6-83fa-891e-ca6b-d3c80e940c21@gmail.com>
<7e9518b0-8e97-bcaf-24ba-e7f013690659@yahoo.co.uk>
<CAPTjJmqEeBWpo7BT_x96sBn86KWBxd6L2x2nY_hwS58B50KsTA@mail.gmail.com>
 by: Ethan Furman - Fri, 3 Mar 2023 17:50 UTC

On 3/3/23 03:32, Chris Angelico wrote:
> On Fri, 3 Mar 2023 at 20:44, Alan Gauld wrote:
>> On 02/03/2023 20:54, Ian Pilcher wrote:

>>> Seems like an FAQ, and I've found a few things on StackOverflow that
>>> discuss the technical differences in edge cases, but I haven't found
>>> anything that talks about which form is considered to be more Pythonic
>>> in those situations where there's no functional difference.
>>
>> I think avoiding dunder methods is generally considered more Pythonic.

Outside of writing dunder methods, I tend to agree.

>> But in this specific case using isinstance() is almost always
>> the better option.

True. IIRC, the only time I haven't used `isinstance` is in `Enum`, where a particular object has to be exactly a tuple
(not a namedtuple, for example) to work correctly.

> Using isinstance is very different from querying the type of an object
> though. They're used for different purposes. And obj.__class__ and
> type(obj) are different too, which is why the OP specifically narrowed
> this down to the situations where you know they're the same.

When writing classes and subclasses, I use `obj.__class__`, `isinstance` otherwise, and rarely `type(obj)` (and then
mostly with `tuple`s, as they're special).

~Ethan~

1
server_pubkey.txt

rocksolid light 0.9.81
clearnet tor