I'm coming from Tcl-world ...

Robert Amesz sheershion at mailexpire.com
Sat Aug 3 23:08:58 EDT 2002

Andreas Leitgeb wrote:

> [common answer:  for(;;)  is rubbish, use "while" or "for ... in"
> instead.] 
> Here, I think, I didn't make my concern clear enough:
>  in C/C++ (and similar in Tcl) I can do the following:
>    for (int i=0,string s="*" ; i<42 ;  i++,s+=s) {
>       ...
>       if (...) continue;
>       ...
>    }
>  More and better examples of where a simple range/list/dictionary-
>  traversal just won't do it, surely exist.

As C-style goes this isn't a very pretty piece of code, especially 
since you seem bent on building a string of 2**42 '*' characters. That 
is a bit over the top, not to mention an exceedingly dull read. ;-) 
Presumably you meant s += "*". That assumed, and also assuming you 
don't modify i in the loop, here's the Python equivalent.

for i in range(42):
    s = "*" * (i + 1)
    if ...:

BTW, using 'continue' is pretty rare, in my experience; 'break' is 
_much_ more common.

>> 4.) "calls by reference":  def f( x ) : x=42
> [common answer: use mutable containers instead, e.g.: ]
> [  def f(x): x[0]=42    ]
> [  x= a list, containing my object as first (and perhaps only)
> element  ] [  f(x);     then,  x[0] outside of f is  still 42 ]
> It's not exactly what I fancied, but near enough :-)

_Don't_ use a one element list: that's misleading to anyone (including 
yourself) who reads that code later. If you don't wan't to use a full-
fledged class (including initializer function) just do:

class simple:

If you really, _really_ need a mutable numeric type you could make one 
yourself by imlementing a class which emulates a numeric type (see ch. 
3.3.6 of the Python manual) but that shouldn't be necessary. More 
importantly: please check _why_ you need a reference to an immutable 
object. Usually you'll find that:

 1 - That immutable object could/should be grouped with other ones to 
form a proper class, or:

 2 - The call-by-reference is needed because you need to have more than 
one return value. Most languages have this limitation, but in Python 
you can return as many values as you like, in a tuple. Then you'd get 
something like:

def func(x):
    x = 42
    return (your_return_value, x)

x = 43
value, x = func(x)

- - -

The examples you give are typical ones if you try to emulate C-style in 
Python. However, if you approach Python as Python you'll find nearly 
all of those problems mysteriously and conveniently disappear. Well, 
perhaps not that mysteriously: Python allows you to program at a much 
higher level of abstraction than C, and with a lot fewer implementation 
details worry about. Programming in Python is many times faster and a 
lot less error prone than doing the same job in C, but if you approach 
Python as C-with-a-strange-syntax you lose much of that advantage.

I'm not quite sure how Python compares to TCL as my experience with 
that language is anything but extensive. The everything-is-a-string 
paradigm is interesting and pretty unique, but also limiting. The 
syntax seems to have been designed to help the interpreter rather than 
the programmer, (IMHO, of course.) I suppose it will take a little 
while for someone with that background to switch styles.

Robert Amesz

More information about the Python-list mailing list