3 ms·
I agree with you about the feeling of fragility, especially with defaultdicts. As for regexes - while they are somewhat clumsy in python, they don't have to be
by asher 14y ago
I agree with you about the feeling of fragility, especially with defaultdicts.
As for regexes - while they are somewhat clumsy in python, they don't have to be defined distantly:
m = re.match(r'(\d{4})', buf)
if m:
print "year: %s" % m.group(1)
The only thing that really bothers me about the above is the need to separate the assignment of m from the test.
Re comprehensions: seems to me they are just Python's version of Perl's map and grep.
grep:
@good = grep { $_->{width} > 9 } @crates;
good = [c for c in crates if c['width'] > 9]
map:
@squares = map { $_ * $_ } @nums;
squares = [i*i for i in nums]
Which is better? I think Ruby's way is the best, and Perl/Python are a wash here.
Now, on the autovivification note, I wonder if we can make a Python class that has the desired behaviors. Like:
c = PerlishStruct()
c['foo'][17]['bar'] = 'baz'
You don't really think it's junk, do you? Perl, Python and Ruby are extremely similar; I'd say Perl's more ergonomic, but messier; Python's cleaner, but sometimes awkward, and Ruby pretty much has the best of both worlds.
- berntb 14y agoYou probably shouldn't do this in an inner loop: :-) m = re.match(r'(\d{4})', buf) 'map' is more general than just the single, convoluted statement of list comprehensions, which really seems as a kludge from unwillingness to accept multi-line lambdas. So something new was invented that had to be learned; then it was argued it is a boon. (Like references in Perl. :-) ) (There is some defaultdict functionality for autovivification in Python iirc.) Yeah, they are all similar. I don't know enough Ruby to have an opinion.