D
D
devpony2015-12-25 01:21:21
Python
devpony, 2015-12-25 01:21:21

Why is filter object not cast to str in python?

Actually, why do you have to fence this construction:

b = ''.join(list(filter(lambda c: c.islower() or c.isdigit(), a)))

instead of
b = str(filter(lambda c: c.islower() or c.isdigit(), a))

Well, after all, why would a non-lazy language suddenly need lazy default functions?
I'm not a holivar for the sake of it, I'm really interested in the reasons for this, in my opinion, not the most successful design.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
abcd0x00, 2015-12-25
@abcd0x00

Actually, why do you have to fence this construction:

And why are you bringing the filtering result to list()?
>>> b = ''.join(filter(None, 'abcde'))
>>> b
'abcde'
>>>

S
Sly_tom_cat ., 2015-12-25
@Sly_tom_cat

Why change anything at all?
>>> filter(lambda c: c.islower() or c.isdigit(), '[email protected]#')
'asd123'
The description of filter clearly states: If iterable is a string or a tuple, the result also has that type;
But this is if you have 'a' - a string. If 'a' is a list, then you first convert it to a string.
But this is true for the second python - in the third filter it is not a function, but an iterator that needs to be processed before you get a string:
>>> ''.join(filter(lambda c: c.islower() or c.isdigit(), '[email protected]#'))
'asd123'
>>>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question