I'm coming from Tcl-world ...

Daniel Fackrell unlearned at DELETETHIS.learn2think.org
Fri Aug 2 16:25:37 CEST 2002


"Andreas Leitgeb" <Andreas.Leitgeb at siemens.at> wrote in message
news:slrnakl3mb.c05.Andreas.Leitgeb at pc7499.gud.siemens.at...
> Hello pythonians!
> I'm somewhat experienced in Tcl-scripting, and I generally still like
> Tcl, but there are some nasty edges in Tcl, which is the reason
> why I had a look at Python.

Welcome!

> I bought a small intro-book about python (it deals with 2.0 to 2.2 with
> some prospects to 3.0), which I've now almost read through, and now there
> are some goodies in Tcl, which I'm sure have their counterparts in Python,
> but I didn't find them so far. (This may also be due to the shortness of
> the book). These are:
>
> 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 ...)

Someone else can probably help you better with this point.  All I can say is
that I'm actually glad of the lack of a C-style 'for' loop in Python, as it
tends to be difficult to read and understand what it is doing unless you do
a lot of it.

For a better answer, try posting an example of the Tcl code that you'd like
to translate into Python, and a specific answer can be given which may bring
much enlightenment.  There are a few of us around that know a little Tcl.

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

I'm not sure what the proper idiom for this is, but I'd suspect that the
better Python users here have an awesome way of handling it, and that their
way makes one wonder why other languages ever bothered with a 'switch'- or
'select/case'-type statement.

> 3.) event-based scripting. (like 'fileevent','after',... in Tcl)
>    I've read somewhere, that with tkinter I get access to tcl's
event-stuff,
>    But I was more after something that also works without Gui and without
>    actually using Tcl through Python.  Is there a builtin module that
>    wraps the select() or poll() systemcall, and invokes the registered
>    functions if "their" respective channel becomes readable/writable
>    or a timer runs out ?

Someone else will have to answer this one, too.

> 4.) "calls by reference": maybe I'm totally misunderstanding pythons
>    function-call-concepts. I want to pass a non-global variable to
>    a subroutine, have it changed there, and find that variable
>    changed in the calling context:
>    something like
>      def f( x ) : x=42
>      f(y); print y
>    of course doesn't work out the way I'm looking for. So what's the
>    easiest, and (if different) what's the python'iest way to do it ?
>    (apart from returning a tuple that contains the changed copy of the
>    object)

You can do this by putting the item to change in a list, dict, or instance
object rather easily (list case shown):

def incr(x):
    x[0] += 1

x=[0]
print x[0]
incr(x)
print x[0]

This will output:
0
1

As always, keep in mind that it should be clear in some way exactly what the
side-effects of a call will be.  Careful choice of names for
functions/methods will help greatly in this.

--
Daniel Fackrell (unlearned at learn2think.org)
When we attempt the impossible, we can experience true growth.





More information about the Python-list mailing list