I'm coming from Tcl-world ...

Hans Nowak wurmy at earthlink.net
Fri Aug 2 21:22:09 EDT 2002


Andreas Leitgeb wrote:
> Andreas Leitgeb <Andreas.Leitgeb at siemens.at> wrote:
> 
>>Hello pythonians!
> 
> thank for all the responses. Because there were so many of them,
> each answering only some parts of the post, I do a followup on my own 
> post ...
> 
> Unfortunately many of the given answers yet failed to make me happy :(

That is understandable, coming from another language and being used to 
different idioms.  But bear in mind that Python is not just a different 
language... it's a different philosophy, and idioms that are common in other 
languages may not be the Right Thing here.

[for loop]
> 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;
>       ...
>    }

This type of loop is very flexible; it's also hard to understand for someone 
not familiar with this construction. I'm not unfamiliar with C and C++, but it 
took me a while to figure out what exactly it was doing, and I'm still not sure 
what happens at the continue.

>  More and better examples of where a simple range/list/dictionary-
>  traversal just won't do it, surely exist.
> 
>  Now, the transscription to while in C/C++ would be:
>  int i=0; string s="*";
>  while (i<42) {
>     ...
>     if (...) goto increment;
>     ...
>   increment:
>     i++; s+=s;
>  }
> 
>  now, Python has no goto (rightly, so), thus by what shall I 
>  replace the "continue", so I run through the incrementing part ?

Why not:

   i, s = 0, "*"
   while i < 42:
       ...
       if not ...:
           ...
       i += 1
       s += s

[switch]
>  However, this has some impediments:
>   First: Only exact matches are possible with dictionaries, whereas
>    I'd have expected from a Python-switch to be able to specify a 
>    comparison-function along, defaulting to exact match/identity.
>   Second:  I want it all in the same scope; I want to set variables 
>    in each "switch-branch" that are still valid outside, 
>    and  I don't want to clutter the namespace with a function for 
>    each switch-branch. (yes, I know, I could reuse function-names after
>    placing the function-object into the dictionary, but...)

The common if-elif-elif-... construct allows for all this flexibility. I know 
you were looking for a different construct besides this, but as it is, Python 
currently doesn't have one.

>>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 :-)

Call by reference is something that is often used in other languages, but which 
doesn't make all that much sense in Python.  Such side effects are generally 
frowned upon, because they make code less clear, among other things.  Usually 
there are better constructs.  A function that returns the new value is one.

I don't know which book you used to learn Python, but it seems it taught you
the right things and common "Pythonic" idioms.  It's only natural that you want
to find ways of doing it that are closer to what you're used to, but eventually
you may find yourself using the other constructs anyway, because they "feel 
better".  Just give them a try.

HTH,

-- 
Hans (base64.decodestring('d3VybXlAZWFydGhsaW5rLm5ldA=='))
# decode for email address ;-)
The Pythonic Quarter:: http://www.awaretek.com/nowak/




More information about the Python-list mailing list