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


Groups > comp.lang.python > #107750

Re: Pythonic style

From Chris Angelico <rosuav@gmail.com>
Newsgroups comp.lang.python
Subject Re: Pythonic style
Date 2016-04-28 13:56 +1000
Message-ID <mailman.168.1461815768.32212.python-list@python.org> (permalink)
References (2 earlier) <572166FA.3020108@icloud.com> <85y47yjxj5.fsf_-_@benfinney.id.au> <57217E73.9040907@icloud.com> <572188F8.7010304@stoneleaf.us> <CAPTjJmrkELf4rb+1UgmPwHxmd+wzp6WLxxN+ut4L3xJuHhmqmA@mail.gmail.com>

Show all headers | View raw


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

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


Thread

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

csiph-web