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


Groups > comp.lang.python > #107750 > unrolled thread

Re: Pythonic style

Started byChris Angelico <rosuav@gmail.com>
First post2016-04-28 13:56 +1000
Last post2016-04-28 09:31 -0400
Articles 13 — 7 participants

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: Pythonic style Chris Angelico <rosuav@gmail.com> - 2016-04-28 13:56 +1000
    Re: Pythonic style Rustom Mody <rustompmody@gmail.com> - 2016-04-27 22:16 -0700
      Re: Pythonic style MRAB <python@mrabarnett.plus.com> - 2016-04-28 12:16 +0100
        Dunder docs again (was Pythonic style) Rustom Mody <rustompmody@gmail.com> - 2016-04-28 04:45 -0700
          Re: Dunder docs again (was Pythonic style) MRAB <python@mrabarnett.plus.com> - 2016-04-28 13:02 +0100
            Re: Dunder docs again (was Pythonic style) Marko Rauhamaa <marko@pacujo.net> - 2016-04-28 15:25 +0300
              Re: Dunder docs again (was Pythonic style) MRAB <python@mrabarnett.plus.com> - 2016-04-28 14:22 +0100
        Re: Pythonic style Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2016-04-29 12:48 +1200
          Re: Pythonic style Chris Angelico <rosuav@gmail.com> - 2016-04-29 10:57 +1000
            Re: Pythonic style Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2016-04-29 18:12 +1200
              Re: Pythonic style Chris Angelico <rosuav@gmail.com> - 2016-04-29 16:45 +1000
          Re: Pythonic style Steven D'Aprano <steve@pearwood.info> - 2016-04-29 19:59 +1000
      Re: Pythonic style Random832 <random832@fastmail.com> - 2016-04-28 09:31 -0400

#107750 — Re: Pythonic style

FromChris Angelico <rosuav@gmail.com>
Date2016-04-28 13:56 +1000
SubjectRe: Pythonic style
Message-ID<mailman.168.1461815768.32212.python-list@python.org>
On Thu, Apr 28, 2016 at 1:52 PM, Ethan Furman <ethan@stoneleaf.us> wrote:
>
> 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__.

My rule of thumb is: Dunders are for defining, not for calling. It's
not a hard-and-fast rule, but it'll get you through 99%+ of
situations.

ChrisA

[toc] | [next] | [standalone]


#107759

FromRustom Mody <rustompmody@gmail.com>
Date2016-04-27 22:16 -0700
Message-ID<c7ca7770-7dfa-463c-b776-cab472d9f79e@googlegroups.com>
In reply to#107750
On Thursday, April 28, 2016 at 9:26:21 AM UTC+5:30, Chris Angelico wrote:
> My rule of thumb is: Dunders are for defining, not for calling. It's
> not a hard-and-fast rule, but it'll get you through 99%+ of
> situations.

Neat and clever.
Should get in the docs somewhere

[toc] | [prev] | [next] | [standalone]


#107779

FromMRAB <python@mrabarnett.plus.com>
Date2016-04-28 12:16 +0100
Message-ID<mailman.183.1461842191.32212.python-list@python.org>
In reply to#107759
On 2016-04-28 06:16, Rustom Mody wrote:
> On Thursday, April 28, 2016 at 9:26:21 AM UTC+5:30, Chris Angelico wrote:
>> My rule of thumb is: Dunders are for defining, not for calling. It's
>> not a hard-and-fast rule, but it'll get you through 99%+ of
>> situations.
>
> Neat and clever.
> Should get in the docs somewhere
>
Is it worthy of being in the Zen of Python?

[toc] | [prev] | [next] | [standalone]


#107780 — Dunder docs again (was Pythonic style)

FromRustom Mody <rustompmody@gmail.com>
Date2016-04-28 04:45 -0700
SubjectDunder docs again (was Pythonic style)
Message-ID<e74de2de-1e5a-448d-9c5c-3852c8a7607a@googlegroups.com>
In reply to#107779
On Thursday, April 28, 2016 at 4:46:43 PM UTC+5:30, MRAB wrote:
> On 2016-04-28 06:16, Rustom Mody wrote:
> > On Thursday, April 28, 2016 at 9:26:21 AM UTC+5:30, Chris Angelico wrote:
> >> My rule of thumb is: Dunders are for defining, not for calling. It's
> >> not a hard-and-fast rule, but it'll get you through 99%+ of
> >> situations.
> >
> > Neat and clever.
> > Should get in the docs somewhere
> >
> Is it worthy of being in the Zen of Python?

:-)
Thats more than I intended... But heck why not?!
I find that dunder methods are poorly documented, especially when compared to
their 'pythonic-ness'.
Ive spoken of this on and off, eg https://mail.python.org/pipermail/python-list/2015-May/691329.html

[toc] | [prev] | [next] | [standalone]


#107782 — Re: Dunder docs again (was Pythonic style)

FromMRAB <python@mrabarnett.plus.com>
Date2016-04-28 13:02 +0100
SubjectRe: Dunder docs again (was Pythonic style)
Message-ID<mailman.184.1461844978.32212.python-list@python.org>
In reply to#107780
On 2016-04-28 12:45, Rustom Mody wrote:
> On Thursday, April 28, 2016 at 4:46:43 PM UTC+5:30, MRAB wrote:
>> On 2016-04-28 06:16, Rustom Mody wrote:
>> > On Thursday, April 28, 2016 at 9:26:21 AM UTC+5:30, Chris Angelico wrote:
>> >> My rule of thumb is: Dunders are for defining, not for calling. It's
>> >> not a hard-and-fast rule, but it'll get you through 99%+ of
>> >> situations.
>> >
>> > Neat and clever.
>> > Should get in the docs somewhere
>> >
>> Is it worthy of being in the Zen of Python?
>
> :-)
> Thats more than I intended... But heck why not?!
> I find that dunder methods are poorly documented, especially when compared to
> their 'pythonic-ness'.
> Ive spoken of this on and off, eg https://mail.python.org/pipermail/python-list/2015-May/691329.html
>
'pythonic-ness'? Surely it's 'pythonicity'! :-)

[toc] | [prev] | [next] | [standalone]


#107783 — Re: Dunder docs again (was Pythonic style)

FromMarko Rauhamaa <marko@pacujo.net>
Date2016-04-28 15:25 +0300
SubjectRe: Dunder docs again (was Pythonic style)
Message-ID<87h9elor7n.fsf@elektro.pacujo.net>
In reply to#107782
MRAB <python@mrabarnett.plus.com>:

> 'pythonic-ness'? Surely it's 'pythonicity'! :-)

Doubt it:

  Full Definition of generic
  [...]

  —generically \-i-k(ə-)lē\ adverb
  —genericness noun

  <URL: http://www.merriam-webster.com/dictionary/generic>


Marko

[toc] | [prev] | [next] | [standalone]


#107786 — Re: Dunder docs again (was Pythonic style)

FromMRAB <python@mrabarnett.plus.com>
Date2016-04-28 14:22 +0100
SubjectRe: Dunder docs again (was Pythonic style)
Message-ID<mailman.186.1461849778.32212.python-list@python.org>
In reply to#107783
On 2016-04-28 13:25, Marko Rauhamaa wrote:
> MRAB <python@mrabarnett.plus.com>:
>
>> 'pythonic-ness'? Surely it's 'pythonicity'! :-)
>
> Doubt it:
>
>   Full Definition of generic
>   [...]
>
>   —generically \-i-k(ə-)lē\ adverb
>   —genericness noun
>
>   <URL: http://www.merriam-webster.com/dictionary/generic>
>
I can point you to 3 alternatives, from oldest to newest:

http://www.oxforddictionaries.com/definition/english/genericalness
http://www.oxforddictionaries.com/definition/english/genericness
http://www.oxforddictionaries.com/definition/english/genericity

[toc] | [prev] | [next] | [standalone]


#107834

FromGregory Ewing <greg.ewing@canterbury.ac.nz>
Date2016-04-29 12:48 +1200
Message-ID<dofpa7Fpm7eU1@mid.individual.net>
In reply to#107779
MRAB wrote:

> Is it worthy of being in the Zen of Python?

+1. Maybe something along the lines of:

    Dunder methods are for defining, not calling.
    Unless you're a dunderhead[1].

[1] Meant in the sense of an enthusiast, cf. gearhead.

-- 
Greg

[toc] | [prev] | [next] | [standalone]


#107835

FromChris Angelico <rosuav@gmail.com>
Date2016-04-29 10:57 +1000
Message-ID<mailman.216.1461891434.32212.python-list@python.org>
In reply to#107834
On Fri, Apr 29, 2016 at 10:48 AM, Gregory Ewing
<greg.ewing@canterbury.ac.nz> wrote:
> MRAB wrote:
>
>> Is it worthy of being in the Zen of Python?
>
>
> +1. Maybe something along the lines of:
>
>    Dunder methods are for defining, not calling.
>    Unless you're a dunderhead[1].
>
> [1] Meant in the sense of an enthusiast, cf. gearhead.

I thought the twentieth zen would never be found?

ChrisA

[toc] | [prev] | [next] | [standalone]


#107843

FromGregory Ewing <greg.ewing@canterbury.ac.nz>
Date2016-04-29 18:12 +1200
Message-ID<dogc9vFst8mU2@mid.individual.net>
In reply to#107835
Chris Angelico wrote:
> I thought the twentieth zen would never be found?

Yes. This will have to be numbered the 21st zen
to maintain that invariant.

-- 
Greg

[toc] | [prev] | [next] | [standalone]


#107844

FromChris Angelico <rosuav@gmail.com>
Date2016-04-29 16:45 +1000
Message-ID<mailman.217.1461912351.32212.python-list@python.org>
In reply to#107843
On Fri, Apr 29, 2016 at 4:12 PM, Gregory Ewing
<greg.ewing@canterbury.ac.nz> wrote:
> Chris Angelico wrote:
>>
>> I thought the twentieth zen would never be found?
>
>
> Yes. This will have to be numbered the 21st zen
> to maintain that invariant.
>

Python for the 21st Century.

In a hundred years, another zen!

ChrisA

[toc] | [prev] | [next] | [standalone]


#107848

FromSteven D'Aprano <steve@pearwood.info>
Date2016-04-29 19:59 +1000
Message-ID<57233071$0$1589$c3e8da3$5496439d@news.astraweb.com>
In reply to#107834
On Fri, 29 Apr 2016 10:48 am, Gregory Ewing wrote:

> MRAB wrote:
> 
>> Is it worthy of being in the Zen of Python?
> 
> +1. Maybe something along the lines of:
> 
>     Dunder methods are for defining, not calling.
>     Unless you're a dunderhead[1].
> 
> [1] Meant in the sense of an enthusiast, cf. gearhead.



I think that the advice to not call dundermethods directly is excellent
advice, but it doesn't belong in the Zen. Look at the Zen: it's all pretty
abstract:

Beautiful is better than ugly.
Explicit is better than implicit.

etc. There's very little[1] concrete advice in the way of specificities such
as:

- don't use floats for money;
- use namedtuple for the equivalent of a C struct or Pascal record;
- composition should be preferred over inheritance;

etc. "Don't use dunders" is much closer to the second, more specific type of
advice which doesn't really fall into the Zen's bailiwick.

Better suited for the Zen would be:

"Not everything needs to be a one-liner."

which is nicely abstract and also completely useless for deciding which
things should and shouldn't be, as good koans ought to be.




[1] By which I mean none.


-- 
Steven

[toc] | [prev] | [next] | [standalone]


#107788

FromRandom832 <random832@fastmail.com>
Date2016-04-28 09:31 -0400
Message-ID<mailman.187.1461850320.32212.python-list@python.org>
In reply to#107759
On Thu, Apr 28, 2016, at 01:16, Rustom Mody wrote:
> On Thursday, April 28, 2016 at 9:26:21 AM UTC+5:30, Chris Angelico wrote:
> > My rule of thumb is: Dunders are for defining, not for calling. It's
> > not a hard-and-fast rule, but it'll get you through 99%+ of
> > situations.
> 
> Neat and clever.
> Should get in the docs somewhere

And even most of the obvious cases where you would want to call one
[e.g. on super] are in the course of implementing one.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web