FAQ

[a,b,c,d] = 1,2,3,4
a
1
b
2
c
3
d
4


I have never seen this syntax before. Is it documented.
Is there a name for that ?


thx

Search Discussions

  • Joel Goldstick at Aug 25, 2015 at 2:30 pm

    On Tue, Aug 25, 2015 at 10:16 AM, ast wrote:
    [a,b,c,d] = 1,2,3,4
    a
    1
    b
    2
    c
    3
    d
    4

    I have never seen this syntax before. Is it documented.
    Is there a name for that ?

    thx
    --
    https://mail.python.org/mailman/listinfo/python-list

    its called list unpacking or packing (?)


    the right side is considered a tuple because of the commas
    a = 1,2,3
    a
    (1, 2, 3)
    a[1]
    2
    >>>




    http://www.developer.com/lang/other/article.php/630101/Learn-to-Program-using-Python-Unpacking-Tuples.htm




    --
    Joel Goldstick
    http://joelgoldstick.com
  • Jean-Michel Pichavant at Aug 25, 2015 at 2:59 pm

    ----- Original Message -----
    From: "Joel Goldstick" <joel.goldstick@gmail.com>
    its called list unpacking or packing (?)

    the right side is considered a tuple because of the commas
    a = 1,2,3
    a
    (1, 2, 3)
    a[1]
    2

    To add to Joel's answer, the right side can be *any* sequence, and is not restricted to lists or tuples.


    a, b, c = (x for x in range(3)) # a generator for instance


    That would be a generator unpacking combined with a tuple packing (is packing restricted to tuples and lists ? probably)


    JM




    -- IMPORTANT NOTICE:


    The contents of this email and any attachments are confidential and may also be privileged. If you are not the intended recipient, please notify the sender immediately and do not disclose the contents to any other person, use it for any purpose, or store or copy the information in any medium. Thank you.
  • Ben Finney at Aug 26, 2015 at 7:29 am

    Jean-Michel Pichavant <jeanmichel@sequans.com> writes:


    -- IMPORTANT NOTICE:

    The contents of this email and any attachments are confidential and
    may also be privileged. If you are not the intended recipient, please
    notify the sender immediately and do not disclose the contents to any
    other person, use it for any purpose, or store or copy the information
    in any medium. Thank you.

    Misleading, intimidating, hostile nonsense. If you want to participate
    here, please do so from a mail system which does not make these
    legalistic demands.


    --
      \ ?It's easy to play any musical instrument: all you have to do |
       `\ is touch the right key at the right time and the instrument |
    _o__) will play itself.? ?Johann Sebastian Bach |
    Ben Finney
  • Jean-Michel Pichavant at Aug 26, 2015 at 9:02 am

    ----- Original Message -----
    From: "Ben Finney" <ben+python@benfinney.id.au>
    The contents of this email and any attachments are confidential and
    may also be privileged. If you are not the intended recipient,
    please
    notify the sender immediately and do not disclose the contents to
    any
    other person, use it for any purpose, or store or copy the
    information
    in any medium. Thank you.
    Misleading, intimidating, hostile nonsense. If you want to
    participate
    here, please do so from a mail system which does not make these
    legalistic demands.

    I agree with you. Unfortunately my request for removing this nonsense has been denied by my employer.
    To the point where I'm restrincting myself from posting from time to time. I will probably restrict myself even more.


    JM




    -- IMPORTANT NOTICE:


    The contents of this email and any attachments are confidential and may also be privileged. If you are not the intended recipient, please notify the sender immediately and do not disclose the contents to any other person, use it for any purpose, or store or copy the information in any medium. Thank you.
  • Terry Reedy at Aug 26, 2015 at 3:09 pm

    On 8/26/2015 5:02 AM, Jean-Michel Pichavant wrote:
    ----- Original Message -----
    From: "Ben Finney" <ben+python@benfinney.id.au>
    The contents of this email and any attachments are confidential and
    may also be privileged. If you are not the intended recipient,
    please
    notify the sender immediately and do not disclose the contents to
    any
    other person, use it for any purpose, or store or copy the
    information
    in any medium. Thank you.
    Misleading, intimidating, hostile nonsense.

    Agreed.

    If you want to participate here, please do so from a mail system
    which does not make these legalistic demands.

    I disagree with prohibiting people encumbered by such systems from
    participating.

    I agree with you. Unfortunately my request for removing this nonsense has been denied by my employer.
    To the point where I'm restrincting myself from posting from time to time. I will probably restrict myself even more.

    How about instead you augment your signature with "Please ignore the
    boilerplate below.

    JM


    -- IMPORTANT NOTICE:

    The contents of this email and any attachments are confidential and may also be privileged. If you are not the intended recipient, please notify the sender immediately and do not disclose the contents to any other person, use it for any purpose, or store or copy the information in any medium. Thank you.



    --
    Terry Jan Reedy
  • Ben Finney at Aug 26, 2015 at 3:29 pm

    Terry Reedy <tjreedy@udel.edu> writes:


    I disagree with prohibiting people encumbered by such systems from
    participating.

    I have no power to prohibit people here. I do strongly request that such
    threatening legalistic screeds stay away from this forum.

    On 8/26/2015 5:02 AM, Jean-Michel Pichavant wrote:
    I agree with you. Unfortunately my request for removing this
    nonsense has been denied by my employer. To the point where I'm
    restrincting myself from posting from time to time. I will probably
    restrict myself even more.
    How about instead you augment your signature with "Please ignore the
    boilerplate below.

    Better: Don't use that mail system to participate here, and instead post
    using a mail system that doesn't add those obnoxious threats.


    --
      \ ?I went camping and borrowed a circus tent by mistake. I didn't |
       `\ notice until I got it set up. People complained because they |
    _o__) couldn't see the lake.? ?Steven Wright |
    Ben Finney
  • Gene Heskett at Aug 26, 2015 at 3:59 pm

    On Wednesday 26 August 2015 11:09:18 Terry Reedy wrote:

    On 8/26/2015 5:02 AM, Jean-Michel Pichavant wrote:
    ----- Original Message -----

    From: "Ben Finney" <ben+python@benfinney.id.au>
    The contents of this email and any attachments are confidential
    and may also be privileged. If you are not the intended recipient,
    please
    notify the sender immediately and do not disclose the contents to
    any
    other person, use it for any purpose, or store or copy the
    information
    in any medium. Thank you.
    Misleading, intimidating, hostile nonsense.
    Agreed.
    If you want to participate here, please do so from a mail system

    which does not make these legalistic demands.
    I disagree with prohibiting people encumbered by such systems from
    participating.

    So do I Terry, but how many years will it take to convince corporate
    legal people that hanging that in a sig is a waste of time & bandwidth.
    AFAIK, no court on _this_ planet has ever upheld the meaning of such a
    notice. All the legal beagles have to keep their presence on the
    payroll justified I guess.

    I agree with you. Unfortunately my request for removing this
    nonsense has been denied by my employer. To the point where I'm
    restrincting myself from posting from time to time. I will probably
    restrict myself even more.

    Please do not restrict yourself further. We'll ignore it (or the fussier
    amoung us should learn to tolerate it) just like we've been ignoring it
    for at least 20 years now.

    How about instead you augment your signature with "Please ignore the
    boilerplate below.
    JM


    -- IMPORTANT NOTICE:

    The contents of this email and any attachments are confidential and
    may also be privileged. If you are not the intended recipient,
    please notify the sender immediately and do not disclose the
    contents to any other person, use it for any purpose, or store or
    copy the information in any medium. Thank you.
    --
    Terry Jan Reedy



    Cheers, Gene Heskett
    --
    "There are four boxes to be used in defense of liberty:
      soap, ballot, jury, and ammo. Please use in that order."
    -Ed Howdershelt (Author)
    Genes Web page <http://geneslinuxbox.net:6309/gene>
  • Rustom Mody at Aug 26, 2015 at 4:04 pm

    On Wednesday, August 26, 2015 at 8:59:44 PM UTC+5:30, Ben Finney wrote:
    Terry Reedy writes:
    I disagree with prohibiting people encumbered by such systems from
    participating.
    I have no power to prohibit people here. I do strongly request that such
    threatening legalistic screeds stay away from this forum.

    Does it really matter?
    We see right here
    - outright spam
    - useful stuff
    - borderline spam viz. interminable grumbles about top-posting, google-groups
    html-mail etc etc etc with nary a python issue in sight


    I dont think anyone would read these with the same eyes (so to speak).
    Why not treat Jean-Michel (and such) mails with a corresponding double-standard?
  • Chris Angelico at Aug 26, 2015 at 4:11 pm

    On Thu, Aug 27, 2015 at 1:09 AM, Terry Reedy wrote:
    How about instead you augment your signature with "Please ignore the
    boilerplate below.

    Heh, I like this! Or:


    """
    -- BOILERPLATE TROPHY COLLECTION


    I am a collector of stupid legal boilerplate. Here's my latest acquisition:
    """


    Then let your legal guys see one of your posts. Maybe they'll relent
    and let you not post it any more.


    ChrisA
  • Jean-Michel Pichavant at Aug 26, 2015 at 4:36 pm

    ----- Original Message -----
    From: "Chris Angelico" <rosuav@gmail.com>
    Cc: python-list at python.org
    Sent: Wednesday, 26 August, 2015 6:11:51 PM
    Subject: Re: Please don't make unfounded legalistic demands (was: [a, b, c, d] = 1, 2, 3, 4)
    On Thu, Aug 27, 2015 at 1:09 AM, Terry Reedy wrote:
    How about instead you augment your signature with "Please ignore
    the
    boilerplate below.
    Heh, I like this! Or:

    """
    -- BOILERPLATE TROPHY COLLECTION

    I am a collector of stupid legal boilerplate. Here's my latest
    acquisition:
    """

    Then let your legal guys see one of your posts. Maybe they'll relent
    and let you not post it any more.

    ChrisA

    I wish I could, problem is, if the mail recipient is within the company, no legal boilerplate is added making the joke fall flat.
    Nice suggestion though.


    I may send it to my IT guys though, I'm sure they'll have a good laugh :)


    JM




    -- IMPORTANT NOTICE:


    The contents of this email and any attachments are confidential and may also be privileged. If you are not the intended recipient, please notify the sender immediately and do not disclose the contents to any other person, use it for any purpose, or store or copy the information in any medium. Thank you.
  • Andy Kubiak at Aug 26, 2015 at 6:11 pm

    I wish I could, problem is, if the mail recipient is within the company,
    no legal boilerplate is added making the joke fall flat.

    What if you could send all your mail to another address on a server you
    control, or can at least run programs on?


    Could you configure all your outgoing mail to include the actual recipient
    list in brackets in the subject line, rather than in the "To:" field?


    So, your outgoing SMTP message, instead of looking like:
    "From: jeanmichel at sequans.com
    To: Foo Barbaz <f.barbaz@foobarbaz.com>, python-list at python.org
    Subject: Please don't make blahblahblah
    I wish I could, but I can't, ergo I won't


    TOP SECRET EMAIL MESSAGE WITH DIRE CONSEQUENCES FOR WANDERING EYES"


    might look more like:
    "From: jeanmichel at sequans.com
    To: otheremailaddress at serveryoucontrol_or_canatleastrunprogramson.com
    Subject: [Foo Barbaz <f.barbaz@foobarbaz.com>, python-list at python.org]
    Please don't make blahblahblah
    I wish I could, but I can't, ergo I won't


    TOP SECRET EMAIL MESSAGE WITH DIRE CONSEQUENCES FOR WANDERING EYES"?


    Then could you use pass the message through a script that uses
    email.message_from_string()
    <https://docs.python.org/3/library/email.parser.html#email.message_from_string>
    to create an <email.message> object, remove the delivery cruft by calling
    <email.message>.__delitem__('Received') on it, recreate the actual "To:"
    field with some to_list = [email.utils.parseaddr
    <https://docs.python.org/3/library/email.util.html>(x) for x in
    string_in_brackets_in_subject] hackery, do a quick


    strained_string = re.sub <https://docs.python.org/3/library/re.html>('TOP
    SECRET EMAIL MESSAGE WITH DIRE CONSEQUENCES FOR WANDERING EYES', '',
    <modifed.email.message>.as_string())?


    Could you then send that off?








    This is not a privileged message. If you're reading this, it could be
    because this is just a text file without any implied security or privacy
    capacity.
    -------------- next part --------------
    An HTML attachment was scrubbed...
    URL: <http://mail.python.org/pipermail/python-list/attachments/20150826/dd21cdb0/attachment.html>
  • Ian Kelly at Aug 26, 2015 at 9:19 pm

    On Wed, Aug 26, 2015 at 12:11 PM, Andy Kubiak wrote:
    What if you could send all your mail to another address on a server you
    control, or can at least run programs on?

    That seems like a lot more hassle than it would be worth.
  • Terry Reedy at Aug 26, 2015 at 9:20 pm
    On 8/26/2015 12:36 PM, Jean-Michel Pichavant wrote:
    [snip]


    Are you allowed to use a newsreader or a mail+newsreader (Outlook
    Express, Thunderbird, <many others>)? If so post through newsgroup
    gmane.comp.python.general at news.gmane.org (as I am).


    --
    Terry Jan Reedy
  • Mark Lawrence at Aug 26, 2015 at 11:19 pm

    On 26/08/2015 22:20, Terry Reedy wrote:
    On 8/26/2015 12:36 PM, Jean-Michel Pichavant wrote:
    [snip]

    Are you allowed to use a newsreader or a mail+newsreader (Outlook
    Express, Thunderbird, <many others>)? If so post through newsgroup
    gmane.comp.python.general at news.gmane.org (as I am).

    The major advantage of this approach is that there are hundreds of
    mailing lists from gmane.comp.python available in a one stop shop.
    Better yet all sorts of goodies are available under news.gmane.org, e.g.
    gwene.org.python.planet or
    gwene.com.activestate.code.feeds.recipes.langs.python


    --
    My fellow Pythonistas, ask not what our language can do for you, ask
    what you can do for our language.


    Mark Lawrence
  • Tim Chase at Aug 27, 2015 at 12:20 am

    On 2015-08-26 17:20, Terry Reedy wrote:
    On 8/26/2015 12:36 PM, Jean-Michel Pichavant wrote:
    Are you allowed to use a newsreader or a mail+newsreader (Outlook
    Express, Thunderbird, <many others>)? If so post through newsgroup
    gmane.comp.python.general at news.gmane.org (as I am).

    Even if NNTP is blocked at work, it's likely that SSH is permitted,
    and thus you'd be able to SSH to an outside machine and run a
    terminal-based news reader like nn/slrn/trn/tin.


    -tkc
  • Jmp at Aug 27, 2015 at 9:39 am

    On 08/26/2015 11:20 PM, Terry Reedy wrote:
    On 8/26/2015 12:36 PM, Jean-Michel Pichavant wrote:
    [snip]

    Are you allowed to use a newsreader or a mail+newsreader (Outlook
    Express, Thunderbird, <many others>)? If so post through newsgroup
    gmane.comp.python.general at news.gmane.org (as I am).

    I screwed already one time let's see if this post go through


    jm
  • Mark Lawrence at Aug 27, 2015 at 10:15 am

    On 27/08/2015 10:39, jmp wrote:
    On 08/26/2015 11:20 PM, Terry Reedy wrote:
    On 8/26/2015 12:36 PM, Jean-Michel Pichavant wrote:
    [snip]

    Are you allowed to use a newsreader or a mail+newsreader (Outlook
    Express, Thunderbird, <many others>)? If so post through newsgroup
    gmane.comp.python.general at news.gmane.org (as I am).
    I screwed already one time let's see if this post go through

    jm

    I'm using the time machine to fool you into thinking that this has all
    ready arrived :)


    --
    My fellow Pythonistas, ask not what our language can do for you, ask
    what you can do for our language.


    Mark Lawrence
  • Grant Edwards at Aug 27, 2015 at 2:14 pm

    On 2015-08-26, Mark Lawrence wrote:
    On 26/08/2015 22:20, Terry Reedy wrote:
    On 8/26/2015 12:36 PM, Jean-Michel Pichavant wrote:
    [snip]

    Are you allowed to use a newsreader or a mail+newsreader (Outlook
    Express, Thunderbird, <many others>)? If so post through newsgroup
    gmane.comp.python.general at news.gmane.org (as I am).
    The major advantage of this approach is that there are hundreds of
    mailing lists from gmane.comp.python available in a one stop shop.
    Better yet all sorts of goodies are available under news.gmane.org,
    e.g. gwene.org.python.planet or
    gwene.com.activestate.code.feeds.recipes.langs.python

    And the NNTP clients and protocol were designed from the ground up to
    handle largish volumes of messages grouped into "lists" and threads.
    Trying to coax that functionality out of e-mail by using list-servers
    and various procmail and e-mail client tricks results is a pretty poor
    substitute.


    --
    Grant Edwards grant.b.edwards Yow! I have many CHARTS
                                       at and DIAGRAMS..
                                   gmail.com
  • Ian Kelly at Aug 27, 2015 at 3:17 pm

    On Thu, Aug 27, 2015 at 8:14 AM, Grant Edwards wrote:
    And the NNTP clients and protocol were designed from the ground up to
    handle largish volumes of messages grouped into "lists" and threads.
    Trying to coax that functionality out of e-mail by using list-servers
    and various procmail and e-mail client tricks results is a pretty poor
    substitute.

    Or you use a client like Gmail that just organizes messages into
    threads for you without the user needing to do anything at all to set
    it up. It works quite well, and I've been using Gmail for so long now
    that I tend to forget that most MUAs *don't* do this automatically.
  • Grant Edwards at Aug 26, 2015 at 4:06 pm

    On 2015-08-26, Terry Reedy wrote:
    On 8/26/2015 5:02 AM, Jean-Michel Pichavant wrote:
    Misleading, intimidating, hostile nonsense.
    Agreed.
    If you want to participate here, please do so from a mail system
    which does not make these legalistic demands.
    I disagree with prohibiting people encumbered by such systems from
    participating.
    I agree with you. Unfortunately my request for removing this nonsense
    has been denied by my employer. To the point where I'm restrincting
    myself from posting from time to time. I will probably restrict
    myself even more.
    How about instead you augment your signature with "Please ignore the
    boilerplate below.

    It's too bad there's just no way to get an email account other than
    through your employer. It's also unfortunate that there's no way to
    to access the mailing list via an NNTP server or web page.


    --
    Grant Edwards grant.b.edwards Yow! I own seven-eighths of
                                       at all the artists in downtown
                                   gmail.com Burbank!
  • Emile van Sebille at Aug 26, 2015 at 8:53 pm

    On 8/26/2015 9:06 AM, Grant Edwards wrote:
    It's also unfortunate that there's no way to
    to access the mailing list via an NNTP server

    Huh? -- gmane.comp.python.general at
    news://nntp.gmane.com:119/gmane.comp.python.general


    Or do you mean by the OP?


    Emile
  • Grant Edwards at Aug 26, 2015 at 9:14 pm

    On 2015-08-26, Emile van Sebille wrote:
    On 8/26/2015 9:06 AM, Grant Edwards wrote:
    It's also unfortunate that there's no way to
    to access the mailing list via an NNTP server
    Huh? -- gmane.comp.python.general at
    news://nntp.gmane.com:119/gmane.comp.python.general

    Or do you mean by the OP?

    I guess I should have used the old <sarcasm> tag. :)


    --
    Grant Edwards grant.b.edwards Yow! LOOK!! Sullen
                                       at American teens wearing
                                   gmail.com MADRAS shorts and "Flock of
                                                        Seagulls" HAIRCUTS!
  • Emile van Sebille at Aug 27, 2015 at 4:29 pm

    On 8/26/2015 2:14 PM, Grant Edwards wrote:
    On 2015-08-26, Emile van Sebille wrote:
    On 8/26/2015 9:06 AM, Grant Edwards wrote:
    It's also unfortunate that there's no way to
    to access the mailing list via an NNTP server
    Huh? -- gmane.comp.python.general at
    news://nntp.gmane.com:119/gmane.comp.python.general

    Or do you mean by the OP?
    I guess I should have used the old <sarcasm> tag. :)



    Damn -- It feels like April 1st all over again. :)


    Emile
  • Mark Lawrence at Aug 26, 2015 at 8:35 pm

    On 26/08/2015 08:29, Ben Finney wrote:
    Jean-Michel Pichavant <jeanmichel@sequans.com> writes:
    -- IMPORTANT NOTICE:

    The contents of this email and any attachments are confidential and
    may also be privileged. If you are not the intended recipient, please
    notify the sender immediately and do not disclose the contents to any
    other person, use it for any purpose, or store or copy the information
    in any medium. Thank you.
    Misleading, intimidating, hostile nonsense. If you want to participate
    here, please do so from a mail system which does not make these
    legalistic demands.

    Haven't you got anything more important to worry about than this, which
    is standard in many organisations in the UK and presumably many other
    parts of the world? I wouldn't even have noticed if you hadn't flagged
    it up in front of everybodys' noses.


    --
    My fellow Pythonistas, ask not what our language can do for you, ask
    what you can do for our language.


    Mark Lawrence
  • Petite Abeille at Aug 26, 2015 at 8:43 pm

    On Aug 26, 2015, at 10:35 PM, Mark Lawrence wrote:

    which is standard in many organisations

    https://en.wikipedia.org/wiki/Creeping_normality
  • Tim Chase at Aug 26, 2015 at 12:21 pm

    On 2015-08-25 16:59, Jean-Michel Pichavant wrote:
    ----- Original Message -----
    From: "Joel Goldstick" <joel.goldstick@gmail.com>
    its called list unpacking or packing (?)

    the right side is considered a tuple because of the commas
    a = 1,2,3
    a
    (1, 2, 3)
    a[1]
    2
    To add to Joel's answer, the right side can be *any* sequence, and
    is not restricted to lists or tuples.

    a, b, c = (x for x in range(3)) # a generator for instance

    Since range() *is* a generator, why not just use


         a, b, c = range(3)


    I do this often for setting constants:


        (
        HR_FILE,
        PHONE_FILE,
        COST_CENTERS_FILE,
        ) = range(3)


    however I have to keep track of how many entries are in there. When
    Py3 introduced variable tuple unpacking, I'd hoped the last one
    wouldn't consume generators, allowing me to do something like


        (
        HR_FILE,
        PHONE_FILE,
        COST_CENTERS,
        *_
        ) = itertools.count()


    so I could insert additional constants and have the list
    automatically adjust. Alas, no such joy. The new Enum class does
    offer an auto-number functionality, but it's clunky, IMHO.


    https://docs.python.org/3/library/enum.html#autonumber




    -tkc
  • Terry Reedy at Aug 26, 2015 at 3:16 pm

    On 8/26/2015 8:21 AM, Tim Chase wrote:


    a, b, c = (x for x in range(3)) # a generator for instance
    Since range() *is* a generator, why not just use

    In Python 3, range is a sequence class with a separate iterator class

    r = range(3)
    r
    range(0, 3)
    iter(r)
    <ran...@...0x00000000034682d0>


    Like all sequences, a range object can be iterated multiple times as a
    new iterator is used each time.

    list(r)
    [0, 1, 2]
    list(r)
    [0, 1, 2]


    --
    Terry Jan Reedy
  • Chris Angelico at Aug 26, 2015 at 1:04 pm

    On Wed, Aug 26, 2015 at 12:59 AM, Jean-Michel Pichavant wrote:
    To add to Joel's answer, the right side can be *any* sequence, and is not restricted to lists or tuples.

    a, b, c = (x for x in range(3)) # a generator for instance

    FWIW, a generator is not a sequence; this works because the right side
    can be any *iterable*, even more general than sequences.


    ChrisA
  • Jean-Michel Pichavant at Aug 26, 2015 at 1:29 pm

    ----- Original Message -----
    From: "Chris Angelico" <rosuav@gmail.com>
    Cc: python-list at python.org
    Sent: Wednesday, 26 August, 2015 3:04:05 PM
    Subject: Re: [a,b,c,d] = 1,2,3,4

    On Wed, Aug 26, 2015 at 12:59 AM, Jean-Michel Pichavant
    wrote:
    To add to Joel's answer, the right side can be *any* sequence, and
    is not restricted to lists or tuples.

    a, b, c = (x for x in range(3)) # a generator for instance
    FWIW, a generator is not a sequence; this works because the right
    side
    can be any *iterable*, even more general than sequences.

    ChrisA

    Sorry about that, I've been mislead by the tutorial which uses sequence where it should have used iterable, so I though they where the same but they're not.


    https://docs.python.org/2/library/stdtypes.html
    "There are seven sequence types: strings, Unicode strings, lists, tuples, bytearrays, buffers, and xrange objects."


    I stand corrected.


    JM




    -- IMPORTANT NOTICE:


    The contents of this email and any attachments are confidential and may also be privileged. If you are not the intended recipient, please notify the sender immediately and do not disclose the contents to any other person, use it for any purpose, or store or copy the information in any medium. Thank you.
  • Cody Piersall at Aug 25, 2015 at 2:32 pm

    On Tue, Aug 25, 2015 at 9:16 AM, ast wrote:


    [a,b,c,d] = 1,2,3,4
    a
    1 b
    2 c
    3 d
    4
    I have never seen this syntax before. Is it documented.
    Is there a name for that ?

    thx
    --
    https://mail.python.org/mailman/listinfo/python-list

    That's called "sequence unpacking"


    Cody
    -------------- next part --------------
    An HTML attachment was scrubbed...
    URL: <http://mail.python.org/pipermail/python-list/attachments/20150825/015a849b/attachment.html>
  • Joel Goldstick at Aug 25, 2015 at 3:05 pm

    On Tue, Aug 25, 2015 at 10:32 AM, Cody Piersall wrote:
    On Tue, Aug 25, 2015 at 9:16 AM, ast wrote:

    [a,b,c,d] = 1,2,3,4
    a
    1
    b
    2
    c
    3
    d
    4

    I have never seen this syntax before. Is it documented.
    Is there a name for that ?

    thx
    --
    https://mail.python.org/mailman/listinfo/python-list

    That's called "sequence unpacking"

    Cody

    --
    https://mail.python.org/mailman/listinfo/python-list

    The original example is one I haven't seen in the wild. One nifty use
    of this feature is to exchange values like this:


    a, b = b, a


    it saves the use of a temporary name
  • Ast at Aug 25, 2015 at 3:18 pm
    "Joel Goldstick" <joel.goldstick@gmail.com> a ?crit dans le message de
    news:mailman.27.1440515128.11709.python-list at python.org...
    On Tue, Aug 25, 2015 at 10:32 AM, Cody Piersall wrote:


    On Tue, Aug 25, 2015 at 9:16 AM, ast wrote:
    The original example is one I haven't seen in the wild.

    I found it using matplotlib


    import numpy as np
    import matplotlib.pyplot as plt


    f, (ax1, ax2) = plt.subplots(2, 1, sharex=True)
  • Jean-Michel Pichavant at Aug 25, 2015 at 2:50 pm

    ----- Original Message -----
    From: "ast" <nomail@invalid.com>
    To: python-list at python.org
    Sent: Tuesday, 25 August, 2015 4:16:17 PM
    Subject: [a,b,c,d] = 1,2,3,4
    [a,b,c,d] = 1,2,3,4
    a
    1
    b
    2
    c
    3
    d
    4

    I have never seen this syntax before. Is it documented.
    Is there a name for that ?

    thx

    You probably have already seen something like:


    a,b,c,d = 1,2,3,4


    which is the same code than yours with the list replaced by a tuple.


    Moreover:
    https://docs.python.org/2/tutorial/datastructures.html


    """
    x, y, z = t
    This is called, appropriately enough, sequence unpacking and works for any sequence on the right-hand side. Sequence unpacking requires the list of variables on the left to have the same number of elements as the length of the sequence. Note that multiple assignment is really just a combination of tuple packing and sequence unpacking.
    """


    It's slightly confusing because it mentions a "list of variable" and then a "tuple packing" while the example uses a tuple.
    Fortunately, lists and tuples can be used in both cases.


    JM




    -- IMPORTANT NOTICE:


    The contents of this email and any attachments are confidential and may also be privileged. If you are not the intended recipient, please notify the sender immediately and do not disclose the contents to any other person, use it for any purpose, or store or copy the information in any medium. Thank you.
  • Ast at Aug 25, 2015 at 3:09 pm
    "Joel Goldstick" <joel.goldstick@gmail.com> a ?crit dans le message de
    news:mailman.23.1440513059.11709.python-list at python.org...
    On Tue, Aug 25, 2015 at 10:16 AM, ast wrote:
    [a,b,c,d] = 1,2,3,4
    a
    1
    b
    2
    c
    3
    d
    4

    I have never seen this syntax before. Is it documented.
    Is there a name for that ?

    thx
    --
    https://mail.python.org/mailman/listinfo/python-list
    its called list unpacking or packing (?)

    the right side is considered a tuple because of the commas
    a = 1,2,3
    a
    (1, 2, 3)
    a[1]
    2

    http://www.developer.com/lang/other/article.php/630101/Learn-to-Program-using-Python-Unpacking-Tuples.htm


    --
    Joel Goldstick
    http://joelgoldstick.com

    Yes you are right, it is related to tuple unpacking
    Here are some useful examples I found.
    http://svn.python.org/projects/python/branches/pep-0384/Lib/test/test_unpack_ex.py
  • Ast at Aug 25, 2015 at 3:13 pm
    "ast" <nomail@invalid.com> a ?crit dans le message de news:55dc853c$0$3083$426a74cc at news.free.fr...
    "Joel Goldstick" <joel.goldstick@gmail.com> a ?crit dans le message de
    news:mailman.23.1440513059.11709.python-list at python.org...
    On Tue, Aug 25, 2015 at 10:16 AM, ast wrote:
    [a,b,c,d] = 1,2,3,4
    a
    1
    b
    2
    c
    3
    d
    4

    I have never seen this syntax before. Is it documented.
    Is there a name for that ?

    thx
    --
    https://mail.python.org/mailman/listinfo/python-list
    its called list unpacking or packing (?)

    the right side is considered a tuple because of the commas
    a = 1,2,3
    a
    (1, 2, 3)
    a[1]
    2

    http://www.developer.com/lang/other/article.php/630101/Learn-to-Program-using-Python-Unpacking-Tuples.htm


    --
    Joel Goldstick
    http://joelgoldstick.com
    Yes you are right, it is related to tuple unpacking
    Here are some useful examples I found.
    http://svn.python.org/projects/python/branches/pep-0384/Lib/test/test_unpack_ex.py

    Unpack in list

    [a, *b, c] = range(5)
    a == 0 and b == [1, 2, 3] and c == 4
         True
  • Jussi Piitulainen at Aug 25, 2015 at 3:24 pm

    "ast" writes:


    [a,b,c,d] = 1,2,3,4
    a
    1
    b
    2
    c
    3
    d
    4

    I have never seen this syntax before. Is it documented.
    Is there a name for that ?

    I remember being unhappy when a similar assignment with round brackets
    turned out to be invalid syntax. Then I learned (in this newsgroup) that
    square brackets would work, and so they did, and then I was happy again,
    though it felt a bit inconsistent. When I try it today, round brackets
    also work, both in 2.6.6 and 3.4.0 - no idea what version it was where
    they failed or if I'm imagining the whole thing.


    The page that Joel Goldstick cited claims that Guido van Rossum himself
    has called the left side of a statement like "w,x,y,z = t3" (no brackets
    on that page) "the list of variables" (but their link to the evidence is
    dead).
  • Skip Montanaro at Aug 25, 2015 at 3:32 pm

    On Tue, Aug 25, 2015 at 10:24 AM, Jussi Piitulainen wrote:


    When I try it today, round brackets
    also work, both in 2.6.6 and 3.4.0 - no idea what version it was where
    they failed or if I'm imagining the whole thing.

    You are imagining the whole thing. Either that, or you had some other
    problem with your tuple unpacking which kept it from working. That has been
    a part of the language as far back as I can remember. I started using
    Python around the 1.0 timeframe.


    Skip
    -------------- next part --------------
    An HTML attachment was scrubbed...
    URL: <http://mail.python.org/pipermail/python-list/attachments/20150825/5f3469e8/attachment.html>
  • Ian Kelly at Aug 25, 2015 at 3:39 pm

    On Tue, Aug 25, 2015 at 9:32 AM, Skip Montanaro wrote:

    On Tue, Aug 25, 2015 at 10:24 AM, Jussi Piitulainen
    wrote:
    When I try it today, round brackets
    also work, both in 2.6.6 and 3.4.0 - no idea what version it was where
    they failed or if I'm imagining the whole thing.

    You are imagining the whole thing. Either that, or you had some other
    problem with your tuple unpacking which kept it from working. That has been
    a part of the language as far back as I can remember. I started using Python
    around the 1.0 timeframe.

    My guess is that Jussi was trying to unpack a sequence of a single
    element like this:


         (a) = some_sequence


    With the result that a is assigned the whole sequence instead of the
    one element, because (a) does not denote a tuple, but merely an
    individual parenthesized expression. Any of these would work in its
    place:


         (a,) = some_sequence
         a, = some_sequence
         [a] = some_sequence
  • Jussi Piitulainen at Aug 25, 2015 at 4:19 pm

    Ian Kelly writes:
    On Tue, Aug 25, 2015 at 9:32 AM, Skip Montanaro wrote:
    On Tue, Aug 25, 2015 at 10:24 AM, Jussi Piitulainen wrote:

    When I try it today, round brackets also work, both in 2.6.6 and
    3.4.0 - no idea what version it was where they failed or if I'm
    imagining the whole thing.
    You are imagining the whole thing. Either that, or you had some other
    problem with your tuple unpacking which kept it from working. That
    has been a part of the language as far back as I can remember. I
    started using Python around the 1.0 timeframe.
    My guess is that Jussi was trying to unpack a sequence of a single
    element like this:

    (a) = some_sequence
    [snip]


    It wasn't that. It was a known number of tab-separated fields that I
    wanted to name individually, like this:


             ID, FORM, LEMMA, POS, MOR, FEATS, \
                 HEAD, REL, DEPS, MISC \
                 = line.split('\t')


    That's from actual code but not necessarily from the place where I first
    tried and failed to use parentheses. It didn't occur to me to try square
    brackets, so I got in the habit of using backslashes as above. (That
    script is dated in January 2015. The something that happened happened
    some months before that. But it may have been a hallucination.)
  • Jussi Piitulainen at Aug 25, 2015 at 3:57 pm

    Skip Montanaro <skip.montanaro@gmail.com> writes:

    On Tue, Aug 25, 2015 at 10:24 AM, Jussi Piitulainen wrote:

    When I try it today, round brackets also work, both in 2.6.6 and
    3.4.0 - no idea what version it was where they failed or if I'm
    imagining the whole thing.
    You are imagining the whole thing. Either that, or you had some other
    problem with your tuple unpacking which kept it from working. That has
    been a part of the language as far back as I can remember. I started
    using Python around the 1.0 timeframe.

    At least they work now, and clearly are intended to work. But I just
    checked and I have scripts with backslash-terminated lines where I'm
    sure I wanted to use those brackets. Could it have been some sort of
    temporary regression? Well, never mind :)

Related Discussions

People

Translate

site design / logo © 2018 Grokbase