anonymous assignment

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Yves Dorfsman

    #16
    Re: anonymous assignment

    Gabriel Genellina wrote:
    >Uses Python 2.6! ;)
    >>
    >
    No need of 2.6 - the above code works since Python 2.2 at least:
    >
    Python 2.2.3 (#42, May 30 2003, 18:12:08) [MSC 32 bit (Intel)] on win32
    Type "help", "copyright" , "credits" or "license" for more information.
    >>>import time
    >>>t=time.local time()
    >>>type(t)
    <type 'time.struct_ti me'>
    >>>t.tm_year
    2008
    >
    (but struct_time objects were printed as regular tuples)
    >
    And not well documented !

    Nice one ! Thanks.

    Comment

    • Yves Dorfsman

      #17
      Re: anonymous assignment

      Ben Finney wrote:
      >> y, _, d, _, _, _, _, _, _ = time.localtime( )
      >But you still have have a variable that's using memory for nothing.
      >
      No, you have one extra unused name binding. The values that you don't
      want to use have *already* been allocated by the time the above
      statement is executed. Name binding doesn't copy the values, it merely
      binds a name to them. There's no "variable" in the above statement.
      >
      But if this happens in the main part of your script, it could take a long
      time before this binding disapear, therefore, the gc won't be able to clean
      that one up. In this particular case, it doesn't really matter (small size),
      but imagine in a case where we are talking of a list of list, with
      potentially large element in the list.


      Yves.

      Comment

      • Yves Dorfsman

        #18
        Re: anonymous assignment

        Scott David Daniels wrote:
        Yves Dorfsman wrote:
        >... Sorry this was a typo (again :-), I meant:
        >d = time.local()
        > y = d[0]
        > d = d[2]
        Then:
        y, d = list(time.local time())[:4:2]
        >
        What is this ?
        Could you point me to a document on this syntax ?

        I've tried it, it works, but I don't understand how.


        Thanks.

        Yves.

        Comment

        • Yves Dorfsman

          #19
          Re: anonymous assignment

          Marc 'BlackJack' Rintsch wrote:
          >> y, _, d, _, _, _, _, _, _ = time.localtime( )
          >But you still have have a variable that's using memory for nothing. I
          >find this unsatisfactory. ..
          >
          Get over it…
          Than what's the point of wanting a better language if every time we run in
          something that looks wrong, or something that could be done better we should
          just "get over it" ?


          Yves.

          Comment

          • Kam-Hung Soh

            #20
            Re: anonymous assignment

            On Tue, 13 May 2008 13:23:30 +1000, Yves Dorfsman <yves@zioup.com wrote:
            Scott David Daniels wrote:
            >Yves Dorfsman wrote:
            >>... Sorry this was a typo (again :-), I meant:
            >>d = time.local()
            >> y = d[0]
            >> d = d[2]
            >Then:
            > y, d = list(time.local time())[:4:2]
            >>
            >
            What is this ?
            Could you point me to a document on this syntax ?
            >
            I've tried it, it works, but I don't understand how.
            >
            >
            Thanks.
            >
            Yves.
            --

            >
            See:

            "Built-in Functions", "slice()",


            l = range(10)
            l
            [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
            l[:4] # first four elements
            [0, 1, 2, 3]
            l[::2] # every second element
            [0, 2, 4, 6, 8]
            l[:4:2] # every second element in the first four elements
            [0, 2]

            --
            Kam-Hung Soh <a href="http://kamhungsoh.com/blog">Software Salariman</a>

            Comment

            • Arnaud Delobelle

              #21
              Re: anonymous assignment

              "Terry Reedy" <tjreedy@udel.e duwrites:
              "Arnaud Delobelle" <arnodel@google mail.comwrote in message
              news:c689625d-b2d2-4268-ae40-15e3c9b78ea2@x4 1g2000hsb.googl egroups.com...
              >
              here is a very sophisticated implementation :)
              >
              >>>def extract(indices , seq):
              ... return tuple(seq[i] for i in indices)
              ...
              >>>y, d = extract((0, 2), time.localtime( ))
              >>>y, d
              (2008, 12)
              >
              =============== ====
              Or a generator version:
              >
              # 3.0
              def extract(iterabl e, indexes):
              # assume indexes are all legal
              enext = enumerate(itera ble).__next__
              i,item = enext()
              for index in indexes:
              while i < index:
              i,item = enext()
              yield item
              >
              import time
              print(list(extr act(time.localt ime(), (0,2))))
              >
              #prints [2008, 12]
              but extract('python ', (3, 1)) won't work!

              --
              Arnaud

              Comment

              • Marc 'BlackJack' Rintsch

                #22
                Re: anonymous assignment

                On Tue, 13 May 2008 03:25:51 +0000, Yves Dorfsman wrote:
                Marc 'BlackJack' Rintsch wrote:
                >>> y, _, d, _, _, _, _, _, _ = time.localtime( )
                >>But you still have have a variable that's using memory for nothing. I
                >>find this unsatisfactory. ..
                >>
                >Get over it…
                >
                Than what's the point of wanting a better language if every time we run in
                something that looks wrong, or something that could be done better we should
                just "get over it" ?
                That advice wasn't for every time something looks wrong but this
                particular one. You can solve it with `operator.itemg etter()` but that
                means importing another module and calling a function which looks much
                more "heavy weight" to me than one reference to an unused object which
                might go away at the end of the function anyway soon.

                Ciao,
                Marc 'BlackJack' Rintsch

                Comment

                • Bruno Desthuilliers

                  #23
                  Re: anonymous assignment

                  Yves Dorfsman a écrit :
                  Marc 'BlackJack' Rintsch wrote:
                  >>> y, _, d, _, _, _, _, _, _ = time.localtime( )
                  >>But you still have have a variable that's using memory for nothing.
                  I'm afraid you just don't understand what Python's "variable" are.
                  You're really worrying about a non-issue here. Seriously.
                  >>I
                  >>find this unsatisfactory. ..
                  >>
                  >Get over it…
                  >
                  Than what's the point of wanting a better language if every time we run
                  in something that looks wrong, or something that could be done better we
                  should just "get over it" ?
                  >
                  No language can be everything to everyone and still retain any kind of
                  consistency. And the Python's team really try to keep a good balance
                  between features and consistency (and so far mostly succeed IHMO).

                  Now, of course, suggesting features or potential improvements *is* a
                  good idea (and FWIW there's an official way to do so : PEPs). But this
                  doesn't mean that each an any such proposition is a good idea by itself,
                  nor that it makes any sense wrt/ Python's design, nor that it's worth
                  implementing it.

                  Comment

                  • Gabriel Genellina

                    #24
                    Re: anonymous assignment

                    En Tue, 13 May 2008 00:21:06 -0300, Yves Dorfsman <yves@zioup.com >
                    escribió:
                    Ben Finney wrote:
                    >
                    >>> y, _, d, _, _, _, _, _, _ = time.localtime( )
                    >>But you still have have a variable that's using memory for nothing.
                    > No, you have one extra unused name binding. The values that you don't
                    >want to use have *already* been allocated by the time the above
                    >statement is executed. Name binding doesn't copy the values, it merely
                    >binds a name to them. There's no "variable" in the above statement.
                    >>
                    >
                    But if this happens in the main part of your script, it could take a
                    long time before this binding disapear, therefore, the gc won't be able
                    to clean that one up. In this particular case, it doesn't really matter
                    (small size), but imagine in a case where we are talking of a list of
                    list, with potentially large element in the list.
                    If you have some reasons to believe it's worth to release that last
                    reference as soon as possible, just do it. `del _` is all you need.
                    (I may have used `del` to remove a local name no more than once a year...)

                    --
                    Gabriel Genellina

                    Comment

                    • Mark Wooding

                      #25
                      Re: anonymous assignment

                      Ben Finney <bignose+hate s-spam@benfinney. id.auwrote:
                      Paul Rubin <http://phr.cx@NOSPAM.i nvalidwrites:
                      >You can just use a variable name than you ignore. It's traditional
                      >to use _ but it's not a special keyword, it's just a another
                      >variable name:
                      >>
                      > y, _, d, _, _, _, _, _, _ = time.localtime( )
                      >
                      It's a terrible name for that purpose, since it doesn't indicate the
                      intention explicitly,
                      There's a lot of tradition behind _ for this purpose, not just in
                      Python. For example, pattern-matching function languages, like ML and
                      Haskell, have been using _ to mean `don't care' for a long time.

                      Just the fact that the name is repeated suggests that you don't care
                      about its value. Besides, it's a good choice for other reasons:
                      because the symbol is short, the variables you do care about stand out
                      clearly.
                      Far better to use the name 'unused' as suggested by Carl Banks earlier
                      today.
                      Other traditional names are `hunoz', `hukairz' and (apparently) `huaskt.

                      But these have much more visual clutter than the humble _. You actually
                      need to do reading to pick out the bindings which you /are/ interested
                      in.

                      -- [mdw]

                      Comment

                      Working...