A bundle of questions from a Python newbie
jason at jorendorff.com
Thu Feb 21 06:56:06 CET 2002
> 2. What is the idea of having in the loop
> for var in range(10):
> the var variable still available after it?
Because it's useful, sometimes.
(It bugs me, too, from an aesthetic standpoint.)
> I don't expect that this solution is adopted (it would break all the
> existing code - ahhh every Nero-like language designer's dream - wreak
> havoc in the citadel of its users and start afresh), what I would like
> to know is how bad/good is this idea.
It's not such a bad idea. I think Ruby does something like this,
with its @tributes. Of course, as you say, it's obviously too late for
Besides, it is said that the explicit "self" makes Python easier to
understand for newbies. And it is known for sure that Guido, the
language inventor, prefers it this way.
> 6. Is there a reason why 3.__abs__() does not work?
Yes, "3." is a floating-point number. If you use extra parentheses,
> 8. Can the id() of an object change? e.g. via the garbage collector
> moving it around?
> 9. What would be better in terms of speed?
> for i in range(<whatever>):
This is much faster than the other one, for lists smaller than
a million elements or so. Got RAM?
Your iterator object could be replaced by xrange(), or by a simple
generator. It'd be faster. But still probably not as fast as range().
> 12. This is not exactly about Python, but about programming in general.
> I keep reading about continuations and coroutines and I would like to
> know, in simple terms if possible, what the heck are they and in what
> ways they differ from generators as they exist in Python.
This is just my understanding of it, and I may be completely wrong.
"The stack" consists of:
* All the same information that gets printed in a Python traceback,
namely which functions have been called and in what order, and
what line of code is currently running;
* AND, all the local variables for all those functions.
(The inspect module can tell you more about this.)
A "continuation" is just a special kind object.
When you create one, it saves a copy of the whole stack.
Later you can "call" the continuation; what happens is kind of like
a massive undo. The current stack gets clobbered. The old stack,
as saved by the continuation, gets restored.
One consequence of this is, no matter what function you're in now,
that part of the stack gets clobbered too, so your program resumes
from the point where the continuation was created!
Sound weird? Yeah.
Sound like generators? Not really. But continuations are the basis
for many other varieties of oddness, and generators are one of the
things that come out of that.
So are coroutines; but I'll leave that one for somebody else. ;)
## Jason Orendorff http://www.jorendorff.com/
More information about the Python-list