Arithmetic sequences in Python

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Paul Rubin

    #61
    Re: Arithmetic sequences in Python

    aleax@mail.comc ast.net (Alex Martelli) writes:[color=blue]
    > I much prefer the current arrangement where dict(a=b,c=d) means {'a':b,
    > 'c':d} -- it's much more congruent to how named arguments work for every
    > other case. Would you force us to quote argument names in EVERY
    > functioncall... ?![/color]

    Ehh, ok. There could be some special marker to evaluate the lhs, but
    the present method is fine too.

    Comment

    • Paul Rubin

      #62
      Re: Arithmetic sequences in Python

      aleax@mail.comc ast.net (Alex Martelli) writes:[color=blue][color=green]
      > > How would you make a one-element list, which we'd currently write as [3]?
      > > Would you have to say list((3,))?[/color]
      >
      > Yep. I don't particularly like the "mandatory trailing comma" in the
      > tuple's display form, mind you, but, if it's good enough for tuples, and
      > good enough for sets (how else would you make a one-element set?),[/color]

      If you really want to get rid of container literals, maybe the best
      way is with constructor functions whose interfaces are slightly
      different from the existing type-coercion functions:

      listx(1,2,3) => [1, 2, 3]
      listx(3) => [3]
      listx(listx(3)) => [[3]]
      dictx((a,b), (c,d)) => {a:b, c:d}
      setx(a,b,c) => Set((a,b,c))

      listx/dictx/setx would be the display forms as well as the constructor forms.

      Comment

      • Tom Anderson

        #63
        Re: Arithmetic sequences in Python

        On Fri, 20 Jan 2006, it was written:
        [color=blue]
        > aleax@mail.comc ast.net (Alex Martelli) writes:
        >[color=green][color=darkred]
        >>> How would you make a one-element list, which we'd currently write as
        >>> [3]? Would you have to say list((3,))?[/color]
        >>
        >> Yep. I don't particularly like the "mandatory trailing comma" in the
        >> tuple's display form, mind you, but, if it's good enough for tuples,
        >> and good enough for sets (how else would you make a one-element set?),[/color]
        >
        > If you really want to get rid of container literals, maybe the best way
        > is with constructor functions whose interfaces are slightly different
        > from the existing type-coercion functions:
        >
        > listx(1,2,3) => [1, 2, 3]
        > listx(3) => [3]
        > listx(listx(3)) => [[3]]
        > dictx((a,b), (c,d)) => {a:b, c:d}
        > setx(a,b,c) => Set((a,b,c))
        >
        > listx/dictx/setx would be the display forms as well as the constructor forms.[/color]

        Could these even replace the current forms? If you want the equivalent of
        list(sometuple) , write list(*sometuple ). With a bit of cleverness down in
        the worky bits, this could be implemented to avoid the apparent overhead
        of unpacking and then repacking the tuple. In fact, in general, it would
        be nice if code like:

        def f(*args):
        fondle(args)

        foo = (1, 2, 3)
        f(*foo)

        Would avoid the unpack/repack.

        The problem is that you then can't easily do something like:

        mytable = ((1, 2, 3), ("a", "b", "c"), (Tone.do, Tone.re, Tone.mi))
        mysecondtable = map(list, mytable)

        Although that's moderately easy to work around with possibly the most
        abstract higher-order-function i've ever written:

        def star(f):
        def starred_f(args) :
        return f(*args)
        return starred_f

        Which lets us write:

        mysecondtable = map(star(list), mytable)

        While we're here, we should also have the natural complement of star, its
        evil mirror universe twin:

        def bearded_star(f) :
        def bearded_starred _f(*args):
        return f(args)
        return bearded_starred _f

        Better names (eg "unpacking" and "packing") would obviously be needed.

        tom

        --
        I might feel irresponsible if you couldn't go almost anywhere and see
        naked, aggressive political maneuvers in iteration, marinating in your
        ideology of choice. That's simply not the case. -- Tycho Brahae

        Comment

        • Paul Rubin

          #64
          Re: Arithmetic sequences in Python

          Tom Anderson <twic@urchin.ea rth.li> writes:[color=blue][color=green]
          > > listx/dictx/setx would be the display forms as well as the constructor forms.[/color]
          >
          > Could these even replace the current forms? If you want the equivalent
          > of list(sometuple) , write list(*sometuple ).[/color]

          The current list function is supposed to be something like a typecast:

          list() = []
          xlist() = [] # ok

          list(list()) = [] # casting a list to a list does nothing
          xlist(xlist()) = [[]] # make a new list, not the same

          list(xrange(4)) = [0,1,2,3]
          xlist(xrange(4) ) = [xrange(4)] # not the same

          list((1,2)) = [1,2]
          xlist((1,2)) = [(1,2)]

          etc.

          Comment

          • Tom Anderson

            #65
            Re: Arithmetic sequences in Python

            On Sat, 21 Jan 2006, it was written:
            [color=blue]
            > Tom Anderson <twic@urchin.ea rth.li> writes:
            >[color=green][color=darkred]
            >>> listx/dictx/setx would be the display forms as well as the constructor
            >>> forms.[/color]
            >>
            >> Could these even replace the current forms? If you want the equivalent
            >> of list(sometuple) , write list(*sometuple ).[/color]
            >
            > The current list function is supposed to be something like a typecast:[/color]

            A what?

            ;-|
            [color=blue]
            > list() = []
            > xlist() = [] # ok
            >
            > list(list()) = [] # casting a list to a list does nothing
            > xlist(xlist()) = [[]] # make a new list, not the same
            >
            > list(xrange(4)) = [0,1,2,3]
            > xlist(xrange(4) ) = [xrange(4)] # not the same
            >
            > list((1,2)) = [1,2]
            > xlist((1,2)) = [(1,2)][/color]

            True, but so what? Is it that it has to be that way, or is it just that it
            happens to be that way now?

            tom

            --
            It's the 21st century, man - we rue _minutes_. -- Benjamin Rosenbaum

            Comment

            • Steve Holden

              #66
              Re: Arithmetic sequences in Python

              Paul Rubin wrote:[color=blue]
              > Tom Anderson <twic@urchin.ea rth.li> writes:
              >[color=green][color=darkred]
              >>>listx/dictx/setx would be the display forms as well as the constructor forms.[/color]
              >>
              >>Could these even replace the current forms? If you want the equivalent
              >>of list(sometuple) , write list(*sometuple ).[/color]
              >
              >
              > The current list function is supposed to be something like a typecast:
              >[/color]
              list() isn't a function, it's a type.
              [color=blue][color=green][color=darkred]
              >>> type(list)[/color][/color][/color]
              <type 'type'>

              I'm not happy about the way the documentation represents types as
              functions, as this obscures the whole essence of Python's object
              orientation.

              [color=blue]
              > list() = []
              > xlist() = [] # ok
              >
              > list(list()) = [] # casting a list to a list does nothing
              > xlist(xlist()) = [[]] # make a new list, not the same
              >
              > list(xrange(4)) = [0,1,2,3]
              > xlist(xrange(4) ) = [xrange(4)] # not the same
              >
              > list((1,2)) = [1,2]
              > xlist((1,2)) = [(1,2)]
              >
              > etc.[/color]

              I presume that here "=" means "evaluates to"?

              regards
              Steve
              --
              Steve Holden +44 150 684 7255 +1 800 494 3119
              Holden Web LLC www.holdenweb.com
              PyCon TX 2006 www.python.org/pycon/

              Comment

              • Christoph Zwerschke

                #67
                Re: Arithmetic sequences in Python

                Alex Martelli wrote:[color=blue][color=green][color=darkred]
                >>>> print set([1,2,3])[/color][/color]
                > set([1, 2, 3])
                >
                > input and output could be identical. Do YOU have any good reason why
                > sets should print out as set(...) and lists should NOT print out as
                > list(...)? Is 'list' somehow "deeper" than 'set', to deserve a special
                > display-form syntax which 'set' doesn't get? Or are you enshrining a
                > historical accident to the level of an erroneously assumed principle?[/color]

                These are valid points, but they lead me to the opposite conclusion: Why
                not let {a,b,c} stand for set([a,b,c])? That would be very intuitive
                since it is the mathematical notation already and since it resembles the
                notation of dictionaries which are similar to sets.

                (This has been probably discussed already. One problem I'm already
                seeing is that {} would be ambiguous.)

                Anyway, I think the fact that the notation for a set is clumsy is no
                good reason to make the notation for a list clumsy as well.

                -- Christoph

                Comment

                • Paul Rubin

                  #68
                  Re: Arithmetic sequences in Python

                  Steve Holden <steve@holdenwe b.com> writes:[color=blue][color=green]
                  > > The current list function is supposed to be something like a
                  > > typecast:
                  > >[/color]
                  > list() isn't a function, it's a type.[/color]

                  I'm not sure what the distinction is supposed to be. "list" is anyway
                  callable, and lambda a:list(a) is certainly a function.
                  [color=blue][color=green]
                  > > xlist((1,2)) = [(1,2)]
                  > > etc.[/color]
                  >
                  > I presume that here "=" means "evaluates to"?[/color]

                  Yeah, although I meant something more informal, like mathematical
                  equivalence.

                  Maybe the preferred spellings for the constructors would use capital
                  letters: List, Dict, Set, instead of listx or xlist or whatever. That
                  would break the current meaning of Set but I hope not much depends on
                  that yet.

                  Comment

                  • Alex Martelli

                    #69
                    Re: Arithmetic sequences in Python

                    Paul Rubin <http://phr.cx@NOSPAM.i nvalid> wrote:
                    [color=blue]
                    > Steve Holden <steve@holdenwe b.com> writes:[color=green][color=darkred]
                    > > > The current list function is supposed to be something like a
                    > > > typecast:
                    > > >[/color]
                    > > list() isn't a function, it's a type.[/color]
                    >
                    > I'm not sure what the distinction is supposed to be. "list" is anyway[/color]

                    You can subclass a type, you can check for it with isinstance, etc, all
                    things you couldn't do if list was still a factory function as in 2.1
                    and back.


                    Alex

                    Comment

                    • Alex Martelli

                      #70
                      Re: Arithmetic sequences in Python

                      Christoph Zwerschke <cito@online.de > wrote:
                      ...[color=blue]
                      > These are valid points, but they lead me to the opposite conclusion: Why
                      > not let {a,b,c} stand for set([a,b,c])? That would be very intuitive[/color]

                      As syntax sugar goes, that would be on a par with the current "dict
                      display" notation, at least.
                      [color=blue]
                      > (This has been probably discussed already. One problem I'm already
                      > seeing is that {} would be ambiguous.)[/color]

                      Yep, using {} for both sets and dicts wouldn't be a good idea. I
                      suspect most core Python developers think of dicts as more fundamental
                      than sets, so... (I may disagree, but I just don't care enough about
                      such syntax sugar to consider even starting a debate about it on
                      python-dev, particularly knowing it would fail anyway).
                      [color=blue]
                      > Anyway, I think the fact that the notation for a set is clumsy is no
                      > good reason to make the notation for a list clumsy as well.[/color]

                      I don't agree that <typename>(<arg uments>) is a clumsy notation, in
                      general; rather, I consider "clumsy" much of the syntax sugar that is
                      traditional in Python. For example, making a shallow copy of a list L
                      with L[:] is what strikes me as clumsy -- list(L) is SO much better.
                      And I vastly prefer dict(a=1,b=2) over the clumsy {'a':1, 'b':2}.

                      I suspect I'm unusual in that being deeply familiar with some notation
                      and perfectly used to it does NOT necessarily make me LIKE that
                      notation, nor does it make me any less disposed to critical reappraisal
                      of it -- the brains of most people do appear to equate habit with
                      appreciation. In the light of my continuous and unceasing critical
                      reappraisal of Python's syntax choices, I am quite convinced that many
                      of them are really brilliant -- with the "display forms" of some
                      built-in types being one area where I find an unusually high density of
                      non-brilliance, AKA clumsiness. But, that's just me.


                      Alex

                      Comment

                      • Christoph Zwerschke

                        #71
                        Re: Arithmetic sequences in Python

                        Alex Martelli wrote:[color=blue]
                        > Yep, using {} for both sets and dicts wouldn't be a good idea. I
                        > suspect most core Python developers think of dicts as more fundamental
                        > than sets, so... (I may disagree, but I just don't care enough about
                        > such syntax sugar to consider even starting a debate about it on
                        > python-dev, particularly knowing it would fail anyway).[/color]

                        I'm still not convinced. At least I'd prefer {a,b,c} over any other
                        proposed solutions (http://wiki.python.org/moin/Python3%2e0Suggestions)
                        such as <a,b,c> or |a,b,c|.

                        You can argue that the notation for sets can be clumsy because they
                        aren't used so much as lists or dicts, but you can also argue the other
                        way around that sets aren't used much because the notation is clumsy
                        (and because they didn't exist from the beginning).

                        For instance, if sets had a simple notation, they could be used in more
                        cases, e.g. replace integer masks (see again


                        pat = re.compile("som e pattern", re.I|re.S|re.X)
                        would become
                        pat = re.compile("som e pattern", {re.I, re.S, re.X})
                        [color=blue]
                        > I don't agree that <typename>(<arg uments>) is a clumsy notation, in
                        > general; rather, I consider "clumsy" much of the syntax sugar that is
                        > traditional in Python.[/color]

                        If you really could write list(a,b,c) instead of list((a,b,c)) I do
                        somewhat agree.
                        [color=blue]
                        > For example, making a shallow copy of a list L
                        > with L[:] is what strikes me as clumsy -- list(L) is SO much better.[/color]

                        Certainly.
                        [color=blue]
                        > And I vastly prefer dict(a=1,b=2) over the clumsy {'a':1, 'b':2}.[/color]

                        Ok, but only as long as you have decent keys...
                        [color=blue]
                        > I suspect I'm unusual in that being deeply familiar with some notation
                        > and perfectly used to it does NOT necessarily make me LIKE that
                        > notation, nor does it make me any less disposed to critical reappraisal
                        > of it -- the brains of most people do appear to equate habit with
                        > appreciation. In the light of my continuous and unceasing critical
                        > reappraisal of Python's syntax choices, I am quite convinced that many
                        > of them are really brilliant -- with the "display forms" of some
                        > built-in types being one area where I find an unusually high density of
                        > non-brilliance, AKA clumsiness. But, that's just me.[/color]

                        Ordinary people are lazy. If we have learned something and got
                        accustomed to it, we don't want to relearn. It is inconvenient. And
                        there is this attitude: "If I had so much trouble learning a clumsy
                        notation, why should future generations have it easier."

                        And in programming languages, you also have the downward compatibility
                        problem. Having to change all your old programs makes people even more
                        dislike any thoughts about changes...

                        -- Christoph

                        Comment

                        • Steven D'Aprano

                          #72
                          Re: Arithmetic sequences in Python

                          On Sun, 22 Jan 2006 16:40:48 -0800, Paul Rubin wrote:
                          [color=blue]
                          > Steve Holden <steve@holdenwe b.com> writes:[color=green][color=darkred]
                          >> > The current list function is supposed to be something like a
                          >> > typecast:
                          >> >[/color]
                          >> list() isn't a function, it's a type.[/color]
                          >
                          > I'm not sure what the distinction is supposed to be. "list" is anyway
                          > callable, and lambda a:list(a) is certainly a function.[/color]


                          class Parrot:
                          def __init__(self):
                          pass

                          Parrot is callable. Is it a function?


                          Types are types, classes are classes, functions are functions.

                          Admittedly I still confused between the various flavours of functions
                          (function, bound method, unbound method, class method, static method...)
                          *wink* but the difference between types and functions is fairly clear.

                          Just don't ask about the difference between type and class... *wink*



                          --
                          Steven.

                          Comment

                          • Bengt Richter

                            #73
                            Re: Arithmetic sequences in Python

                            On Mon, 23 Jan 2006 21:43:16 +1100, Steven D'Aprano <steve@REMOVETH IScyber.com.au> wrote:
                            [color=blue]
                            >On Sun, 22 Jan 2006 16:40:48 -0800, Paul Rubin wrote:
                            >[color=green]
                            >> Steve Holden <steve@holdenwe b.com> writes:[color=darkred]
                            >>> > The current list function is supposed to be something like a
                            >>> > typecast:
                            >>> >
                            >>> list() isn't a function, it's a type.[/color]
                            >>
                            >> I'm not sure what the distinction is supposed to be. "list" is anyway
                            >> callable, and lambda a:list(a) is certainly a function.[/color]
                            >
                            >
                            >class Parrot:
                            > def __init__(self):
                            > pass
                            >
                            >Parrot is callable. Is it a function?
                            >[/color]
                            No. It is an object that inherits a __call__ method that makes it callable with a (<args>) source syntax trailer
                            in a similar way to an object created with a def (which is a function in python convention, and which
                            also inherits a __call__ method), but the similarity does not make Parrot a function:
                            [color=blue][color=green][color=darkred]
                            >>> class Parrot:[/color][/color][/color]
                            ... def __init__(self):
                            ... pass
                            ...
                            (BTW you could have inherited a do-nothing __init__ ;-)
                            [color=blue][color=green][color=darkred]
                            >>> type(Parrot).mr o()[/color][/color][/color]
                            [<type 'classobj'>, <type 'object'>][color=blue][color=green][color=darkred]
                            >>> type(Parrot).mr o()[0].__call__[/color][/color][/color]
                            <slot wrapper '__call__' of 'classobj' objects>
                            Or, showing more explicitly where it comes from:[color=blue][color=green][color=darkred]
                            >>> type(Parrot).mr o()[0].__dict__['__call__'][/color][/color][/color]
                            <slot wrapper '__call__' of 'classobj' objects>

                            Invoked:[color=blue][color=green][color=darkred]
                            >>> type(Parrot).mr o()[0].__dict__['__call__'](Parrot)[/color][/color][/color]
                            <__main__.Parro t instance at 0x02EF340C>

                            Hm, actually that is like calling the im_func of the unbound method of a newstyle class ...
                            I think maybe actually Parrot() causes
                            [color=blue][color=green][color=darkred]
                            >>> type(Parrot).mr o()[0].__dict__['__call__'].__get__(Parrot , type(Parrot))()[/color][/color][/color]
                            <__main__.Parro t instance at 0x02EF398C>

                            A function is also an object with a __call__ method. E.g., compare above the line with
                            calling foo (after definition just below) the long way:
                            [color=blue][color=green][color=darkred]
                            >>> def foo(): return 'returned by foo'[/color][/color][/color]
                            ...[color=blue][color=green][color=darkred]
                            >>> type(foo).mro()[0].__dict__['__call__'].__get__(foo, type(foo))()[/color][/color][/color]
                            'returned by foo'

                            Playing with that a little:[color=blue][color=green][color=darkred]
                            >>> type(foo).mro()[/color][/color][/color]
                            [<type 'function'>, <type 'object'>][color=blue][color=green][color=darkred]
                            >>> type(foo).mro()[0][/color][/color][/color]
                            <type 'function'>[color=blue][color=green][color=darkred]
                            >>> type(foo).mro()[0].__call__[/color][/color][/color]
                            <slot wrapper '__call__' of 'function' objects>[color=blue][color=green][color=darkred]
                            >>> type(foo).mro()[0].__call__(foo)[/color][/color][/color]
                            'returned by foo'[color=blue][color=green][color=darkred]
                            >>> foo.__call__[/color][/color][/color]
                            <method-wrapper object at 0x02EF340C>[color=blue][color=green][color=darkred]
                            >>> foo.__call__()[/color][/color][/color]
                            'returned by foo'[color=blue][color=green][color=darkred]
                            >>> type(foo).mro()[0].__call__.__get __[/color][/color][/color]
                            <method-wrapper object at 0x02EF340C>[color=blue][color=green][color=darkred]
                            >>> type(foo).mro()[0].__call__.__get __(foo, type(foo))[/color][/color][/color]
                            <method-wrapper object at 0x02EF39AC>[color=blue][color=green][color=darkred]
                            >>> type(foo).mro()[0].__call__.__get __(foo, type(foo))()[/color][/color][/color]
                            'returned by foo'
                            or[color=blue][color=green][color=darkred]
                            >>> foo()[/color][/color][/color]
                            'returned by foo'
                            [color=blue]
                            >
                            >Types are types, classes are classes, functions are functions.[/color]
                            classes seem to be classobjs, designed to implement classic class behavior
                            but using the new machinery to achieve compatible integration.
                            [color=blue]
                            >
                            >Admittedly I still confused between the various flavours of functions
                            >(function, bound method, unbound method, class method, static method...)
                            >*wink* but the difference between types and functions is fairly clear.
                            >
                            >Just don't ask about the difference between type and class... *wink*
                            >[/color]
                            Why not? ;-)

                            Regards,
                            Bengt Richter

                            Comment

                            Working...