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


Groups > comp.lang.python > #107747

Re: Pythonic style

From Ethan Furman <ethan@stoneleaf.us>
Newsgroups comp.lang.python
Subject Re: Pythonic style
Date 2016-04-27 20:52 -0700
Message-ID <mailman.165.1461815470.32212.python-list@python.org> (permalink)
References (1 earlier) <CALwzidn0SN5yax3bfmTPDTxBvfr+bnVNzeUe3dZYrS9ji2WCtw@mail.gmail.com> <572166FA.3020108@icloud.com> <85y47yjxj5.fsf_-_@benfinney.id.au> <57217E73.9040907@icloud.com> <572188F8.7010304@stoneleaf.us>

Show all headers | 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


Thread

Re: Pythonic style Ethan Furman <ethan@stoneleaf.us> - 2016-04-27 20:52 -0700

csiph-web