Re: [Tutor] Lists

2011-06-10 Thread eizetov
Vincent, you should also try to avoid naming your lists a 'list'. There's a  
couple dozen words in Python that are reserved 'for system use':



import keyword
print(keyword.kwlist)

['False', 'None', 'True', 'and', 'as', 'assert', 'break', 'class', 'continue', 
'def', 'del', 'elif', 'else', 'except', 'finally', 'for', 'from', 'global', 
'if', 'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or', 'pass', 'raise', 
'return', 'try', 'while', 'with', 'yield']

'List' is actually not listed here, however, using it as a variable name is  
still bad:



list('spam')

['s', 'p', 'a', 'm']

list = '42'
list('spam')

Traceback (most recent call last):
File pyshell#26, line 1, in module
list('spam')
TypeError: 'str' object is not callable

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] String formatting question with 's'.format()

2011-06-08 Thread eizetov

I'm working through the 'Learn Python' book by Mark Lutz, in this example:


somelist = list('SPAM')
parts = somelist[0], somelist[-1], somelist[1:3]
'first={0}, last={1}, middle={2}'.format(*parts)

first=S, last=M, middle=['P', 'A']

why do we need the '*' at 'parts'. I know we need it, because otherwise it  
gives an error:


Traceback (most recent call last):
File pyshell#17, line 1, in module
'first={0}, last={1}, middle={2}'.format(parts)
IndexError: tuple index out of range

Still, wouldn't python basically see 'parts' and insert the actual tuple  
instead of the variable 'parts'? How does the machine think?
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor