Unpaking Tuple

Hans Mulder hansmu at xs4all.nl
Mon Nov 19 02:56:29 CET 2012

On 9/10/12 08:07:32, Bob Martin wrote:
> in 682592 20121008 232126 "Prasad, Ramit" <ramit.prasad at jpmorgan.com> wrote:
>> Thomas Bach wrote:=0D=0A> Hi there,=0D=0A> =0D=0A> On Sat, Oct 06, 2012 at =
>> 03:08:38PM +0000, Steven D'Aprano wrote:=0D=0A> >=0D=0A> > my_tuple =3D my_=
>> tuple[:4]=0D=0A> > a,b,c,d =3D my_tuple if len(my_tuple) =3D=3D 4 else (my_=
>> tuple + (None,)*4)[:4]=0D=0A> >=0D=0A> =0D=0A> Are you sure this works as y=
>> ou expect? I just stumbled over the following:=0D=0A> =0D=0A> $ python=0D=
>> =0A> Python 3=2E2=2E3 (default, Jun 25 2012, 23:10:56)=0D=0A> [GCC 4=2E7=2E=
>> 1] on linux2=0D=0A> Type "help", "copyright", "credits" or "license" for mo=
>> re information=2E=0D=0A> >>> split =3D ['foo', 'bar']=0D=0A> >>> head, tail=
>> =3D split if len(split) =3D=3D 2 else split[0], None=0D=0A> >>> head=0D=0A=
>>> ['foo', 'bar']=0D=0A> >>> tail=0D=0A> >>>=0D=0A> =0D=0A> I don't get it! =
>> Could someone help me, please? Why is head not 'foo'=0D=0A> and tail not 'b=
>> ar'?=0D=0A> =0D=0A> Regards,=0D=0A> 	Thomas=0D=0A> --=0D=0A=0D=0AI think yo=
>> u just need to wrap the else in parenthesis so the=0D=0Aelse clause is trea=
>> ted as a tuple=2E Without the parenthesis =0D=0AI believe it is grouping th=
>> e code like this=2E=0D=0A=0D=0Ahead, tail =3D (split if len(split) =3D=3D 2=
>> else split[0] ), None=0D=0A=0D=0AYou want:=0D=0Ahead, tail =3D split if le=
>> n(split) =3D=3D 2 else (split[0], None )=0D=0A=0D=0A=0D=0ARamit=0D=0AThis e=
>> mail is confidential and subject to important disclaimers and=0D=0Aconditio=
>> ns including on offers for the purchase or sale of=0D=0Asecurities, accurac=
>> y and completeness of information, viruses,=0D=0Aconfidentiality, legal pri=
>> vilege, and legal entity disclaimers,=0D=0Aavailable at http://www=2Ejpmorg=
>> an=2Ecom/pages/disclosures/email=2E
> How does one unpack this post?  ;-)

How about:

    print re.sub('^>* ', '', this_post, flags=re.M).decode('quopri')

Hope this helps,

-- HansM

More information about the Python-list mailing list