Filtering out non-readable characters
steve at REMOVETHIScyber.com.au
Sun Jul 17 07:08:12 CEST 2005
On Sat, 16 Jul 2005 16:42:58 -0400, Peter Hansen wrote:
> Steven D'Aprano wrote:
>> On Sat, 16 Jul 2005 10:25:29 -0400, Peter Hansen wrote:
>>>Bengt Richter wrote:
>>>> >>> identity = ''.join([chr(i) for i in xrange(256)])
>>>And note that with Python 2.4, in each case the above square brackets
>>>are unnecessary (though harmless), because of the arrival of "generator
>>>expressions" in the language.
>> But to use generator expressions, wouldn't you need an extra pair of round
>> eg identity = ''.join( ( chr(i) for i in xrange(256) ) )
> Come on, Steven. Don't tell us you didn't have access to a Python
> interpreter to check before you posted:
Er, as I wrote in my post:
who is still using Python 2.3, and probably will be for quite some time"
So, no, I didn't have access to a Python interpreter running version 2.4.
I take it then that generator expressions work quite differently
than list comprehensions? The equivalent "implied delimiters" for a list
comprehension would be something like this:
>>> L = [1, 2, 3]
>>> L[ i for i in range(2) ]
File "<stdin>", line 1
L[ i for i in range(2) ]
SyntaxError: invalid syntax
which is a very different result from:
>>> L[ [i for i in range(2)] ]
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: list indices must be integers
In other words, a list comprehension must have the [ ] delimiters to be
recognised as a list comprehension, EVEN IF the square brackets are there
from some other element. But a generator expression doesn't care where the
round brackets come from, so long as they are there: they can be part of
the function call.
I hope that makes sense to you.
More information about the Python-list