syntax philosophy

T

Tuang

Terry Reedy said:
Tuang said:
$dict{$color}++
to count up what you find.

Your wish is Python's command:

class cdict(dict):
def __getitem__(self, key):
try:
return dict.__getitem__(self, key)
except KeyError:
return 0

h=cdict()
for item in [1,1,2,3,2,3,1,4]:
h[item]+=1
{1: 3, 2: 2, 3: 2, 4: 1}
$dict{$salesperson} += $amount

Trivial modification:

t=cdict()
for key,amt in ((1,2), (1,3), (2,5), (3,1), (3,2), (3,3)):
t[key] += amt
{1: 5, 2: 5, 3: 6}

Nice. I like that. It appears that in Python it pays to think at a
slightly higher level of abstraction than in Perl. Put a little extra
time into creating a little tool for the job the first time, then
reuse it the next time. I do that with Perl code snippets, but using
small classes or functions instead appeals to me.
 
C

Carl D Cravens

I just started learning Python over the weekend, and I have to say that it
was very frustrating at first. This thread came at just the right time,
and it's been very enlightening. Understanding Python's approach has
helped me accept and appreciate why it does things the way it does.

I was originally a C advocate, lured to Perl by the rapid
development... I'm a Unix sysadmin and Perl was perfect for me. But as I
look at doing larger projects, I find Perl's modules, scoping and OO to be
esoteric. Python looks like just the ticket.

Thanks to everyone who contributed.
 
V

Ville Vainio

Nice. I like that. It appears that in Python it pays to think at a
slightly higher level of abstraction than in Perl. Put a little extra
time into creating a little tool for the job the first time, then
reuse it the next time. I do that with Perl code snippets, but using
small classes or functions instead appeals to me.

Yep, w/ Python, if you want magic, you just implement it yourself or
use a premade module that implements the magic. It's all doable, it's
just not the standard, out of the box way. IMO the "convenience"
argument for having the magic built into the language is worthless,
because getting to that magic is easy enough w/ the tools we already
have. doRE("var=~s/hi/hello/i"), anyone? :)

And having something as an out-of-the-box __builtins__ is still
infinitely preferable to having an implementation of something
intermingled w/ the code generator for the language, even w/ the
minimal sacrifice of performance.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,609
Members
45,254
Latest member
Top Crypto TwitterChannel

Latest Threads

Top