Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #107747
| Path | csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail |
|---|---|
| From | Ethan Furman <ethan@stoneleaf.us> |
| Newsgroups | comp.lang.python |
| Subject | Re: Pythonic style |
| Date | Wed, 27 Apr 2016 20:52:24 -0700 |
| Lines | 34 |
| Message-ID | <mailman.165.1461815470.32212.python-list@python.org> (permalink) |
| References | <5720357B.4060009@icloud.com> <CALwzidn0SN5yax3bfmTPDTxBvfr+bnVNzeUe3dZYrS9ji2WCtw@mail.gmail.com> <572166FA.3020108@icloud.com> <85y47yjxj5.fsf_-_@benfinney.id.au> <57217E73.9040907@icloud.com> <572188F8.7010304@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 YUNC4N/ILy8DZxD5Xjh8Tgm5pYZ1eG3nob82GQS5x9Ew== |
| Return-Path | <ethan@stoneleaf.us> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.003 |
| X-Spam-Evidence | '*H*': 0.99; '*S*': 0.00; 'essentially': 0.04; 'from:addr:ethan': 0.09; 'from:addr:stoneleaf.us': 0.09; 'from:name:ethan furman': 0.09; 'message-id:@stoneleaf.us': 0.09; 'methods,': 0.09; 'python': 0.10; '7:07': 0.16; 'attributes,': 0.16; 'dictionary.': 0.16; 'magic': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'wrote:': 0.16; '>>>': 0.20; 'latter': 0.22; 'trying': 0.22; 'this:': 0.23; 'header:In-Reply-To:1': 0.24; 'header:User-Agent:1': 0.26; 'equivalent': 0.27; 'separate': 0.27; 'invoke': 0.29; 'short,': 0.29; '~ethan~': 0.29; 'classes': 0.30; 'code': 0.30; 'skip:s 30': 0.31; 'maybe': 0.33; 'point': 0.33; 'behind': 0.35; 'should': 0.36; 'instead': 0.36; 'there': 0.36; 'to:addr:python-list': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'say': 0.37; 'turned': 0.38; 'to:addr:python.org': 0.40; 'your': 0.60; 'no.': 0.62; 'more': 0.63; 'special': 0.73; "(don't": 0.84 |
| User-Agent | Mozilla/5.0 (X11; Linux i686; rv:31.0) Gecko/20100101 Thunderbird/31.2.0 |
| In-Reply-To | <57217E73.9040907@icloud.com> |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.22 |
| 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> |
| X-Mailman-Original-Message-ID | <572188F8.7010304@stoneleaf.us> |
| X-Mailman-Original-References | <5720357B.4060009@icloud.com> <CALwzidn0SN5yax3bfmTPDTxBvfr+bnVNzeUe3dZYrS9ji2WCtw@mail.gmail.com> <572166FA.3020108@icloud.com> <85y47yjxj5.fsf_-_@benfinney.id.au> <57217E73.9040907@icloud.com> |
| Xref | csiph.com comp.lang.python:107747 |
Show key headers only | View raw
On 04/27/2016 08:07 PM, Christopher Reimer wrote:
> On 4/27/2016 7:07 PM, Ben Finney wrote:
>> Ian Kelly wrote:
>>> self.__dict__ = {'key', 'value'}
>>>
>>> is essentially equivalent to:
>>>
>>> self.key = value
>>
>> I would say the latter is more Pythonic, because it:
>>
>> [snip]
>>
>> * Uses the built-in mechanisms of Python (don't invoke magic attributes,
>> instead use the system that makes use of them behind the scenes).
>
> In short, my original code before I turned it into a separate
> dictionary. *sigh*
No.
The point Ben was trying to make is this: you should never* call
__dunder__ methods in normal code; there is no need to do so:
- use len(), not __len__()
- use next(), not __next__()
- use some_instance.an_attribute, not some_instance.__dict__['an_attribute']
--
~Ethan~
* Okay, maybe /almost/ never. About the only time you need to is when
giving your classes special methods, such as __add__ or __repr__.
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
Re: Pythonic style Ethan Furman <ethan@stoneleaf.us> - 2016-04-27 20:52 -0700
csiph-web