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


Groups > comp.lang.python > #38573

Re: LangWart: Method congestion from mutate multiplicty

Newsgroups comp.lang.python
Date 2013-02-10 06:33 -0800
References <680e50a4-6569-49cf-b369-0be450545d50@googlegroups.com> <5115c455$0$6574$c3e8da3$5496439d@news.astraweb.com>
Message-ID <e4f70f88-9713-4cc5-a269-85452a9425e9@googlegroups.com> (permalink)
Subject Re: LangWart: Method congestion from mutate multiplicty
From 88888 Dihedral <dihedral88888@googlemail.com>

Show all headers | View raw


Steven D'Aprano於 2013年2月9日星期六UTC+8上午11時36分52秒寫道:
> Rick Johnson wrote:
> 
> 
> 
> > The solution is simple. Do not offer the "copy-mutate" methods and force
> 
> > all mutation to happen in-place:
> 
> > 
> 
> > py> l = [1,2,3]
> 
> > py> l.reverse
> 
> > py> l
> 
> > [3,2,1]
> 
> > 
> 
> > If the user wants a "mutated copy" he should explicitly create a new
> 
> > object and then apply the correct mutator method:
> 
> > 
> 
> > py> a1 = [1,2,3]
> 
> > py> a2 = list(a1).reverse()
> 
> 
> 
> 
> 
> Oh wow, Rick has re-discovered programming in Python during the mid to late
> 
> 1990s!
> 
> 
> 
> I was there, and I remember what it was like. For about a month, you try
> 
> hard to follow Rick's prescription. Then you realise that with a small
> 
> helper function, you can halve the amount of code it takes to do a common
> 
> operation:
> 
> 
> 
> def reversed(sequence):
> 
>     seq = list(sequence)
> 
>     seq.reverse()
> 
>     return seq
> 
> 
> 
> 
> 
> Soon you've copied this reversed() function into all your projects. And of
> 
> course, they start to diverge... in project A, you only care about lists.
> 
> In project B, you realise that you also need to support tuples and strings:
> 
> 
> 
> 
> 
> def reversed(sequence):
> 
>     seq = sequence[:]
> 
>     try:
> 
>         seq.reverse()
> 
>     except AttributeError:
> 
>         seq = seq[::-1]
> 
>     return seq
> 
> 
Will a temprary new list be formed here?
If it is not necessary, I'll prefer a reverse 
generator for all lists to save the heap space
and the GC burden.


> 
> which in project C you realise can be shortened:
> 
> 
> 
> def reversed(sequence):
> 
>     return sequence[::-1]
> 
> 
> 
> 
> 
> until you get to project D when you realise that you also want this to work
> 
> on dicts:
> 
> 
> 
> def reversed(sequence):
> 
>     everything = list(sequence)
> 
>     return everything[::-1]
> 
> 
> 
> 
> 
> and then in project E you wonder why reversed(string) returns a list:
> 
> 
> 
> def reversed(sequence):
> 
>     everything = list(sequence)[::-1]
> 
>     if isinstance(sequence, tuple):
> 
>         return tuple(everything)
> 
>     elif isinstance(sequence, str):
> 
>         return ''.join(everything)
> 
>     return everything
> 
> 
> 
> 
> 
> and then finally you learn about iterators and generators and become more
> 
> comfortable with a flow-based programming paradigm and generators:
> 
> 
> 
> def reversed(sequence):
> 
>     for item in list(sequence)[::-1]:
> 
>         yield item
> 
> 
> 
> at which point you realise that, hell, this is so useful that pretty much
> 
> everyone has implemented it a dozen times or more in their own projects,
> 
> and you start to agitate for it to be added to the builtins so that there
> 
> is *one* implementation, done *right*, that everyone can use.
> 
> 
> 
> And then you get told that Guido's time machine has struck again, because
> 
> Python has already had this since Python 2.4.
> 
> 
> 
> 
> 
> 
> 
> -- 
> 
> Steven

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


Thread

LangWart: Method congestion from mutate multiplicty Rick Johnson <rantingrickjohnson@gmail.com> - 2013-02-08 17:50 -0800
  Re: LangWart: Method congestion from mutate multiplicty Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-02-09 14:36 +1100
    Re: LangWart: Method congestion from mutate multiplicty Rick Johnson <rantingrickjohnson@gmail.com> - 2013-02-09 19:54 -0800
      Re: LangWart: Method congestion from mutate multiplicty Chris Angelico <rosuav@gmail.com> - 2013-02-10 15:20 +1100
      Re: LangWart: Method congestion from mutate multiplicty Mark Janssen <dreamingforward@gmail.com> - 2013-02-09 20:53 -0800
        Re: LangWart: Method congestion from mutate multiplicty Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-02-11 01:29 +1100
          Re: LangWart: Method congestion from mutate multiplicty Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2013-02-10 14:03 -0500
            Re: LangWart: Method congestion from mutate multiplicty Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-02-11 11:11 +1100
          Re: LangWart: Method congestion from mutate multiplicty Mark Janssen <dreamingforward@gmail.com> - 2013-02-10 13:28 -0800
            Re: LangWart: Method congestion from mutate multiplicty Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-02-11 11:10 +1100
              Re: LangWart: Method congestion from mutate multiplicty Mark Janssen <dreamingforward@gmail.com> - 2013-02-10 17:14 -0800
          Re: LangWart: Method congestion from mutate multiplicty Chris Angelico <rosuav@gmail.com> - 2013-02-11 08:51 +1100
          Re: LangWart: Method congestion from mutate multiplicty Mark Janssen <dreamingforward@gmail.com> - 2013-02-10 14:01 -0800
      Re: LangWart: Method congestion from mutate multiplicty Terry Reedy <tjreedy@udel.edu> - 2013-02-10 03:39 -0500
        Re: LangWart: Method congestion from mutate multiplicty Rick Johnson <rantingrickjohnson@gmail.com> - 2013-02-10 10:45 -0800
          Re: LangWart: Method congestion from mutate multiplicty Terry Reedy <tjreedy@udel.edu> - 2013-02-10 16:44 -0500
          Re: LangWart: Method congestion from mutate multiplicty Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-02-11 11:11 +1100
        Re: LangWart: Method congestion from mutate multiplicty Rick Johnson <rantingrickjohnson@gmail.com> - 2013-02-10 10:45 -0800
      Re: LangWart: Method congestion from mutate multiplicty Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-02-10 22:29 +1100
        Re: LangWart: Method congestion from mutate multiplicty Chris Angelico <rosuav@gmail.com> - 2013-02-11 00:24 +1100
          Re: LangWart: Method congestion from mutate multiplicty Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-02-11 01:00 +1100
        Re: LangWart: Method congestion from mutate multiplicty Tim Chase <python.list@tim.thechases.com> - 2013-02-10 07:52 -0600
        Re: LangWart: Method congestion from mutate multiplicty Rick Johnson <rantingrickjohnson@gmail.com> - 2013-02-10 15:59 -0800
          Re: LangWart: Method congestion from mutate multiplicty Tim Chase <python.list@tim.thechases.com> - 2013-02-10 18:12 -0600
            Re: LangWart: Method congestion from mutate multiplicty Rick Johnson <rantingrickjohnson@gmail.com> - 2013-02-10 17:24 -0800
            Re: LangWart: Method congestion from mutate multiplicty Rick Johnson <rantingrickjohnson@gmail.com> - 2013-02-10 17:24 -0800
          Re: LangWart: Method congestion from mutate multiplicty Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-02-11 11:36 +1100
            Re: LangWart: Method congestion from mutate multiplicty MRAB <python@mrabarnett.plus.com> - 2013-02-11 02:03 +0000
            Re: LangWart: Method congestion from mutate multiplicty Rick Johnson <rantingrickjohnson@gmail.com> - 2013-02-11 04:53 -0800
              Re: LangWart: Method congestion from mutate multiplicty Chris Angelico <rosuav@gmail.com> - 2013-02-12 00:27 +1100
                Re: LangWart: Method congestion from mutate multiplicty Rick Johnson <rantingrickjohnson@gmail.com> - 2013-02-11 20:55 -0800
                Re: LangWart: Method congestion from mutate multiplicty Mark Janssen <dreamingforward@gmail.com> - 2013-02-11 21:28 -0800
                Re: LangWart: Method congestion from mutate multiplicty Rick Johnson <rantingrickjohnson@gmail.com> - 2013-02-12 12:46 -0800
                Re: LangWart: Method congestion from mutate multiplicty Rick Johnson <rantingrickjohnson@gmail.com> - 2013-02-12 12:46 -0800
                Re: LangWart: Method congestion from mutate multiplicty Rick Johnson <rantingrickjohnson@gmail.com> - 2013-02-11 20:55 -0800
          Re: LangWart: Method congestion from mutate multiplicty alex23 <wuwei23@gmail.com> - 2013-02-10 18:05 -0800
            Re: LangWart: Method congestion from mutate multiplicty Mark Lawrence <breamoreboy@yahoo.co.uk> - 2013-02-11 07:19 +0000
            Re: LangWart: Method congestion from mutate multiplicty Chris Angelico <rosuav@gmail.com> - 2013-02-11 18:24 +1100
            Re: LangWart: Method congestion from mutate multiplicty Mark Lawrence <breamoreboy@yahoo.co.uk> - 2013-02-11 07:35 +0000
            Re: LangWart: Method congestion from mutate multiplicty Tim Chase <tim@thechases.com> - 2013-02-11 06:04 -0600
            Re: LangWart: Method congestion from mutate multiplicty Serhiy Storchaka <storchaka@gmail.com> - 2013-02-11 19:07 +0200
      Re: LangWart: Method congestion from mutate multiplicty Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2013-02-10 13:30 +0000
        Re: LangWart: Method congestion from mutate multiplicty Rick Johnson <rantingrickjohnson@gmail.com> - 2013-02-10 16:25 -0800
          Re: LangWart: Method congestion from mutate multiplicty Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-02-11 11:42 +1100
        Re: LangWart: Method congestion from mutate multiplicty Rick Johnson <rantingrickjohnson@gmail.com> - 2013-02-10 16:25 -0800
      Re: LangWart: Method congestion from mutate multiplicty Mark Janssen <dreamingforward@gmail.com> - 2013-02-10 10:45 -0800
    Re: LangWart: Method congestion from mutate multiplicty 88888 Dihedral <dihedral88888@googlemail.com> - 2013-02-10 06:33 -0800
  Re: LangWart: Method congestion from mutate multiplicty Chris Angelico <rosuav@gmail.com> - 2013-02-09 15:51 +1100
    Re: LangWart: Method congestion from mutate multiplicty alex23 <wuwei23@gmail.com> - 2013-02-10 17:56 -0800
      Re: LangWart: Method congestion from mutate multiplicty Chris Angelico <rosuav@gmail.com> - 2013-02-12 19:06 +1100
  Re: LangWart: Method congestion from mutate multiplicty Neil Hodgson <nhodgson@iinet.net.au> - 2013-02-10 20:53 +1100
    Re: LangWart: Method congestion from mutate multiplicty Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-02-10 22:30 +1100
    Re: LangWart: Method congestion from mutate multiplicty Rick Johnson <rantingrickjohnson@gmail.com> - 2013-02-10 10:55 -0800
      Re: LangWart: Method congestion from mutate multiplicty Neil Hodgson <nhodgson@iinet.net.au> - 2013-02-11 08:57 +1100

csiph-web