Zope 3.0, and why I won't use it
Ian Bicking
ianb at colorstudy.com
Tue Nov 16 14:00:52 EST 2004
Josiah Carlson wrote:
>>Since the actual need for type-checking is extremely rare, I contend it
>>should not _at all_ be a candidate for a syntax to facilitate it. Since
>>the actual need for adaptation is much wider, I contend it's the one
>>obvious choice for the "nice syntax" if any.
>
>
> Sounds reasonable, but there will be a group of people who will beg and
> plead for type checking, because they want "static type checking on
> function call and return".
Yes, but they've been begging for it for years. They seem insistant
that this Python Thing will never catch on without these important features.
Anyway, a much more general and useful feature would be contracts.
Which is just to say, a formal place to put constraints on input and
output for a function. Contracts could include type checking --
sometimes quite validly -- but not necessarily. There's no type that
can indicate "only valid username strings" or "only integers contrained
by some global variable".
> @ multi_dispatch_to_methods
> class A(object):
> dispatch = dispatch_factory()
>
> @ dispatch(int)
> def foo(x): ...
>
> @ dispatch(float)
> def foo(x): ...
>
>
> It may have a couple warts, but it is implementable today.
Phillip Eby just did this, in like the last week. It's very cool:
http://peak.telecommunity.com/DevCenter/VisitorRevisited
http://www.eby-sarna.com/pipermail/peak/2004-November/001916.html
It looks similar to what you were thinking:
class A(object):
@dispatch.on('x')
def foo(x):
"""This is a dummy function, for documentation purposes"""
@foo.when(int)
def foo(x): ...
@foo.when(float)
def foo(x): ...
Of course, you can use interfaces in addition to types, and you can also
use arbitrary expressions (e.g., @foo.when("x % 2") and @foo.when("not x
% 2") to dispatch odd and even numbers separately).
--
Ian Bicking / ianb at colorstudy.com / http://blog.ianbicking.org
More information about the Python-list
mailing list