x, = y (???)

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

    x, = y (???)




    I just came across an assignment of the form

    x, = y

    where y is a string (in case it matters).

    1. What's the meaning of the comma in the LHS of the assignment?
    2. How could I have found this out on my own?

    (Regarding (2) above, I consulted the index of several Python
    reference books but I could not find the answer to (1). I hope to
    find a better Python reference!)

    TIA!

    kynn

    --
    NOTE: In my address everything before the first period is backwards;
    and the last period, and everything after it, should be discarded.
  • Erik Max Francis

    #2
    Re: x, = y (???)

    kj wrote:
    I just came across an assignment of the form
    >
    x, = y
    >
    where y is a string (in case it matters).
    >
    1. What's the meaning of the comma in the LHS of the assignment?
    2. How could I have found this out on my own?
    >
    (Regarding (2) above, I consulted the index of several Python
    reference books but I could not find the answer to (1). I hope to
    find a better Python reference!)
    It's unpacking a 1-tuple:

    (x,) = y

    The parentheses here are not necessary and are sometimes left out.

    --
    Erik Max Francis && max@alcyone.com && http://www.alcyone.com/max/
    San Jose, CA, USA && 37 18 N 121 57 W && AIM, Y!M erikmaxfrancis
    The United States, while they wish for war with no nation, will buy
    peace with none. -- James Madison

    Comment

    • kj

      #3
      Re: x, = y (???)

      In <crydndTBLuLcBu LVnZ2dnUVZ_jWdn Z2d@speakeasy.n etErik Max Francis <max@alcyone.co mwrites:
      >kj wrote:
      >I just came across an assignment of the form
      >>
      > x, = y
      >>
      >where y is a string (in case it matters).
      >>
      >1. What's the meaning of the comma in the LHS of the assignment?
      >It's unpacking a 1-tuple:
      > (x,) = y
      >The parentheses here are not necessary and are sometimes left out.
      I still don't get it. If we write

      y = 'Y'
      x, = y

      what's the difference now between x and y? And if there's no
      difference, what's the point of performing such "unpacking" ?

      TIA!

      kynn


      --
      NOTE: In my address everything before the first period is backwards;
      and the last period, and everything after it, should be discarded.

      Comment

      • Jonathan Gardner

        #4
        Re: x, = y (???)

        On Jul 17, 12:55 pm, kj <so...@987jk.co m.invalidwrote:
        I still don't get it.  If we write
        >
          y  = 'Y'
          x, = y
        >
        what's the difference now between x and y?  And if there's no
        difference, what's the point of performing such "unpacking" ?
        >
        Try:

        y = "abc"
        x, = y

        You were unpacking y into ('Y',)

        Comment

        • Jonathan Gardner

          #5
          Re: x, = y (???)

          On Jul 17, 12:55 pm, kj <so...@987jk.co m.invalidwrote:
          what's the point of performing such "unpacking" ?
          record = [name, address, telephone]

          ...

          name, address, telephone = record

          Comment

          • Chris Mellon

            #6
            Re: x, = y (???)

            On Thu, Jul 17, 2008 at 2:55 PM, kj <socyl@987jk.co m.invalidwrote:
            In <crydndTBLuLcBu LVnZ2dnUVZ_jWdn Z2d@speakeasy.n etErik Max Francis <max@alcyone.co mwrites:
            >
            >>kj wrote:
            >
            >>I just came across an assignment of the form
            >>>
            >> x, = y
            >>>
            >>where y is a string (in case it matters).
            >>>
            >>1. What's the meaning of the comma in the LHS of the assignment?
            >
            >>It's unpacking a 1-tuple:
            >
            > (x,) = y
            >
            >>The parentheses here are not necessary and are sometimes left out.
            >
            I still don't get it. If we write
            >
            y = 'Y'
            x, = y
            >
            what's the difference now between x and y? And if there's no
            difference, what's the point of performing such "unpacking" ?
            >
            TIA!
            >>y = (1,)
            >>y
            (1,)
            >>x, = y
            >>x
            1
            >>>

            Comment

            • Matthew Woodcraft

              #7
              Re: x, = y (???)

              kj wrote:
              I still don't get it. If we write
              >
              y = 'Y'
              x, = y
              >
              what's the difference now between x and y? And if there's no
              difference, what's the point of performing such "unpacking" ?
              If y really is is a string, I think it's likely that the line you came
              across was a typo.

              In the case you give above, there's no difference at the end between x
              and y.

              If y had length other than 1, the second line would raise an exception,
              so it's not the same as plain "x = y". But if that's the intended effect,
              it's a daft way to write it.

              -M-

              Comment

              • Duncan Booth

                #8
                Re: x, = y (???)

                kj <socyl@987jk.co m.invalidwrote:
                I still don't get it. If we write
                >
                y = 'Y'
                x, = y
                >
                what's the difference now between x and y? And if there's no
                difference, what's the point of performing such "unpacking" ?
                None whatsoever when the string has only one character, but with 2
                characters it becomes an obfuscated way of checking that there was only one
                character:
                >>x, = 'a'
                >>x, = 'ab'
                Traceback (most recent call last):
                File "<pyshell#2 9>", line 1, in <module>
                x, = 'ab'
                ValueError: too many values to unpack

                Comment

                • Terry Reedy

                  #9
                  Re: x, = y (???)



                  kj wrote:
                  >
                  >
                  I just came across an assignment of the form
                  >
                  x, = y
                  >
                  where y is a string (in case it matters).
                  >
                  1. What's the meaning of the comma in the LHS of the assignment?
                  2. How could I have found this out on my own?
                  1.Experiment with the interactive interpreter. It is sooo easy.
                  >>x, = '1'
                  >>x
                  '1'
                  >>x, = '12'
                  Traceback (most recent call last):
                  File "<pyshell#5 3>", line 1, in <module>
                  x, = '12'
                  ValueError: too many values to unpack
                  >>x,y = '12'
                  >>x,y
                  ('1', '2')
                  >>x, = 3
                  Traceback (most recent call last):
                  File "<pyshell#4 5>", line 1, in <module>
                  x, = 3
                  TypeError: 'int' object is not iterable
                  >>*x, = [3]
                  >>x
                  [3]

                  2. Read the Python docs which come with the interpreter.
                  LanguageReferen ce/SimpleStatement s/AssignmentState ments:
                  "If the target list is a comma-separated list of targets:
                  ....
                  Else: The object must be a sequence with the same number of items as
                  there are targets in the target list, and the items are assigned, from
                  left to right, to the corresponding targets."
                  which is exactly the behavior observed above.

                  Comment

                  • DaveM

                    #10
                    Re: x, = y (???)

                    On Thu, 17 Jul 2008 17:32:30 -0400, Terry Reedy <tjreedy@udel.e duwrote:

                    >*x, = [3]
                    >x
                    >[3]
                    What does *x signify?

                    DaveM

                    Comment

                    • alex23

                      #11
                      Re: x, = y (???)

                      On Jul 18, 8:01 am, DaveM <asm...@dsl.pip ex.comwrote:
                      On Thu, 17 Jul 2008 17:32:30 -0400, Terry Reedy <tjre...@udel.e duwrote:
                      >>*x, = [3]
                      >>x
                      [3]
                      >
                      What does *x signify?
                      Mostly that Terry is using Python 3.0.

                      See: http://www.python.org/dev/peps/pep-3132/

                      Comment

                      • Andrew Freeman

                        #12
                        Re: x, = y (???)

                        kj wrote:
                        >
                        I just came across an assignment of the form
                        >
                        x, = y
                        >
                        where y is a string (in case it matters).
                        >
                        1. What's the meaning of the comma in the LHS of the assignment?
                        2. How could I have found this out on my own?
                        >
                        (Regarding (2) above, I consulted the index of several Python
                        reference books but I could not find the answer to (1). I hope to
                        find a better Python reference!)
                        >
                        TIA!
                        >
                        kynn
                        >
                        Try this:
                        >>y = 'abc'
                        >>type(y)
                        <type 'str'>
                        >>type((y,))
                        <type 'tuple'>
                        >>y = y, # y, is just short for (y,) you *have* to use a coma in 1
                        length tuples
                        >>y
                        ('abc',)
                        >>type(y)
                        <type 'tuple'>
                        >>y[0]
                        'abc'
                        >>x, = y #same as x = y[0] OR more verbosely (x,) = (y,)
                        >>x
                        'abc'
                        >>type(x)
                        <type 'str'>

                        Maybe that will hape you understand what the comma is for?

                        Comment

                        • kj

                          #13
                          Re: x, = y (???)

                          In <BZr*yX+hs@news .chiark.greenen d.org.ukMatthew Woodcraft <mattheww@chiar k.greenend.org. ukwrites:
                          >kj wrote:
                          >I still don't get it. If we write
                          >>
                          > y = 'Y'
                          > x, = y
                          >>
                          >what's the difference now between x and y? And if there's no
                          >difference, what's the point of performing such "unpacking" ?
                          >If y really is is a string, I think it's likely that the line you came
                          >across was a typo.

                          OK, this is the best explanation I've seen for the code I'm talking about.

                          This code may be found at:



                          in the definition of the function eliminate. Here's the fragment:

                          elif len(values[s]) == 1:
                          ## If there is only one value (d2) left in square, remove it from peers
                          d2, = values[s]

                          Now, in the assignment, values[s] *is* a string of length 1. So
                          this assignment appears to me entirely equivalent (in its ultimate
                          effect) to the more straightforward

                          d2 = values[s]

                          ....but, since I'm a noob, I thought I'd ask :-)

                          Thanks!

                          kynn
                          --
                          NOTE: In my address everything before the first period is backwards;
                          and the last period, and everything after it, should be discarded.

                          Comment

                          Working...