Overloading and? was <RE: Should I prefer an external database>
ianb at colorstudy.com
Fri Apr 25 07:54:38 CEST 2003
On Fri, 2003-04-25 at 00:11, Andrew Dalke wrote:
> Can that object be saved? Eg, what does this do?
> class Spam:
> def __and__(self, other):
> self.saved = other
> return other()
> spam = Spam()
> a = spam and 1/0:
> except ZeroDivisionError:
Arrr matey... there be continuations. Dark waters indeed!
> For some languages, like Smalltalk and Ruby, this makes sense.
> Those languages support code blocks, which can be passed
> around and executed. But Python does not.
I suspect that in Smalltalk you cannot actually overload and:. It is a
quiet optimization that Smalltalks do, that turn particular methods into
special bytecode. Things like ifTrue:ifFalse:, and to:do: are not
compiled to method calls, and I suspect the same is true of and:. It's
just too inefficient.
I don't actually know how Ruby does and...? Syntactically distinct like
Python, or as a method call like Smalltalk? I expect more like
Python... either way I'm sure they all do the same thing deep down.
> Were Python to have code blocks, then it should be applied
> more generally to Python code. Consider the if/else PEP of
> a couple months ago. With code blocks, the following would
> be possible
> ifelse(a != 0, 1/a, None)
> which would pass the 1/a and None as independent code
> blocks, to be executed based on the evaluation of a != 0.
Of course, ifelse would be best implemented as a macro, not with code
blocks (which presumably would require some syntax, like
ifelse(a != 0, `1/a`, `None`)). Macros are of course compile-time, not
runtime, and Python deliberately does very little of interest during the
compile. I think there are actually some serious problems introduced
because of its runtime nature, but nevertheless that's how Python is.
More information about the Python-list