[Tutor] Language truce

Roeland Rengelink r.b.rigilink@chello.nl
Fri, 29 Jun 2001 01:31:41 +0200


I haven't followed this completely, so maybe this is gibberish, but..

Danny Yoo wrote:
> 
> On Thu, 28 Jun 2001, Michael Powe wrote:
> 
> > >>>>> "Danny" == Danny Yoo <dyoo@hkn.eecs.berkeley.edu> writes:
> >
> >     Danny> On Thu, 28 Jun 2001, Michael Powe wrote:
> >
> >     >> A couple things I mentioned, like you can't increment through
> >     >> loops.
> >
> >     Danny> I'm a little confused by what you mean by "incrementing
> >     Danny> through loops".  Can you give an example of what you mean?
> >
> > The standard C (for i=0; i < n; i++), which uses the increment
> > operator (i++) which is not present in python.  I'm sure there must be
> > some discussion somewhere about why the decision was made to leave
> > this out, I just have not come across it.
> >
> > Yes, as someone else points out, you can use range(), but obviously
> > that is not the same thing. (In some cases, it may be functionally
> 
> You're right that converting C's:
> 
>     for (i = 0; i < n ; i++) { ... }
> 
> isn't quite the same as Python's
> 
>     for i in range(n): ...
> 

In comparing how Python and C handle this I think it's very important to
ask what's happening in {...}. Yes, the above seems a pretty
straightforward translation, though not literally the same. However,
this misses the point of the true differences between C and Python.
Consider:

for (i=0; i<n; i++){
   a = array_of_as[i];
   do_something(a);
}

This is one of the idiomatic usages of the C loop construct, i.e. as an
index. You would never translate this into Python as:

for i in range(n):
    a = list_of_as[i]
    do_something(a)

but as:

for a in list_of_as:
    do_something(a)

The difference in paradigm between Python and C here, is not illustrated
by the difference in the C example and the first Python example, but by
the difference between the first Python example and the second. (I think
though, that even these differences are too small to constitute a
paradigm difference)

By the way, the most literal translation of the general C loop into
Python is:

i = 0
while i<n:
    ...
    i+=1 

This may look like a very different way of expressing it, but it does
_exactly_ the same thing, both conceptually, and implementation-wise.
This construct also allows for playing around with i in the body of the
loop ("increment through loops").

Now, you may rarely see this in practice, but that's not because Python
lacks an increment operator ;)

<snip>

> > k = k+1
> >
> > as a loop counter to just be unacceptably clumsy.  Forcing me to use
> > that construct tells me that I should not be designing my program that
> > way.
> 

I agree with your conclusion, although I would say that you don't use
loops in Python as you'd do in C not so much because Python doesn't have
an increment operator, but because C doesn't have lists/tuples/strings.

In fact, I think that that's the comment I really wanted to make. I
think you're right that there are different paradigms underlying C and
Python, but this is not reflected in the rather incidental presence or
absence of the syntactic sugarish case statement and increment operator.
Rather, think object-oriented vs imperative, high-level vs low-level,
dynamic vs static, objects vs pointers, etc. etc.

<snip> 
> > My 'teach-yourself-python' project is an address book.  I happen to
> > need one and I don't like anything that's already available.
> > Unfortunately, the python books just aren't making it as instructional
> > tools.  I know quite a bit about python syntax &c and practically
> > nothing about actually writing programs in python.  Anyway, I have the
> > data entry part and the write-to-file parts completed -- that was
> > easy.  But, the file-reading and data storage parts are more
> > interesting.  In C, I would just use a struct and a linked list and
> > write/read the structs to/from a binary file -- I've done all that
> > type of stuff before.
> 

In Python a class in stead of a struct, and a list or dict in stead of a
linked list.
Of course you can build a general linked list data-structure in Python
too. It's actually 
a fun way to learn about classes. In practice I think you'll find that
you rarely need one.

> It's nice to see a fellow C programmer here!  Yeah, it will take a while
> to get used to Python's way of doing things, but it's very much worth it
> --- I've found that my own C programs look a lot better after learning
> Python.
> 

I can second that (and my C++ has improved too)

Just some ramblings,

Roeland
-- 
r.b.rigilink@chello.nl

"Half of what I say is nonsense. Unfortunately I don't know which half"