I'm coming from Tcl-world ...

Andreas Leitgeb Andreas.Leitgeb at siemens.at
Fri Aug 2 12:48:27 EDT 2002


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

> 1.) A 'for'-loop like in Tcl or in C, where you have separate
>    Init-, condition and increment-blocks, that control the loop.
>    (Of course, there's while, which can emulate all, but with that,
>    getting "continue" straight (that is: jump to the "increment-part" 
>    of the body) is probably some hassle ...)

[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.

 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 ?

 How could python tell the increment-part from the rest of the body ?
 the previous continue might be arbitrarily widely nested inside
 some hierarchy of if's ...


> 2.) A 'switch'-thing: like a big if-elif-elif-elif-...-else but which
>    evaluates its expression only once ... and then does all the comparisons.

[common answer:  use dictionaries with functions as values ...]

 coming from Tcl,  dictionaries are not new to me, and even in Tcl,
  I could start out like:
  set switch(42) {puts 42}
  set switch(666) {puts devil}
  eval $switch($myvalue);  # $myvalue being the switch-expression.
 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...)

I'll have a look at the PEP-url given;  thanks.


> 3.) event-based scripting. (like 'fileevent','after',... in Tcl)
[common answer: yes, there are modules: asyncore and select]
Thanks a lot, I'll have a look at them.

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


> PS: [performance comparison among versions 1.5, 2.0 and 2.2 of python]
[answer:  yes, there was a change in memory-allocation when appending 
 to lists]
That explains it :-)
Thus, whenever I intend to handle large data, I'd better upgrade
to a version at least 2.2 or so.

btw. the last attempt (on version 2.0) has taken 200 cpu-minutes so far 
  and still running on a 900MHz, with a job that took about 10 seconds 
  with 2.2 on a 300Mhz-machine.  :-)

-- 
Newsflash: Sproingy made it to the ground !
  read more ... <http://avl.enemy.org/sproingy/>



More information about the Python-list mailing list