[Tutor] multiple assignment

Kent Johnson kent37 at tds.net
Tue Oct 18 13:55:58 CEST 2005


Vincent Gulinao wrote:
> Haha. Cool.
> 
> I think it's better than...
> 
> if str.find(" "):
>     str1, str2, str3 = str.split(" ", 3)
> else:
>     str1, str2, str3, rest = str, None, None, None

Less buggy, too - your version will fail if str="1 2" or "1 2 3 4"

BTW don't use str as the name of a string, it is already the name of the string *type*. In general you should avoid using the names of built-ins for variable names.

Kent

PS Please reply on list so others can benefit

> 
> On 10/18/05, *Kent Johnson* <kent37 at tds.net <mailto:kent37 at tds.net>> wrote:
>     AFAIK there is no pretty solution but this works:
>     str1, str2, str3 = (s.split(" ", 3) + [None]*3)[:3]



More information about the Tutor mailing list