[Python-ideas] Improving the expressivity of function annotations
Carl M. Johnson
cmjohnson.mailinglist at gmail.com
Mon Apr 4 11:09:23 CEST 2011
>
> Anyway this is where "structural types" come in: defining a type not by its
> name but by its shape (a set of methods and properties, and their
> signatures).
>
Again, this can be handled by defining (enough) abcs, but it soon becomes
> tiresome as ABCs are still pretty verbose to write and I don't doubt there
> would soon be about 5 million versions of ``Readable`` (requirement of a
> single no-args method ``read`` producing bytes). As a result, the ability to
> define a type as a set of methods (and their signatures) without having to
> give it an explicit name would — I think — be great.
OK, reading this again, maybe you mean something more like a Java interface?
@interface
class Writer:
def write(output: bytes) -> None:
"Takes in bytes, returns None. Raises Foo on error."
pass
class DuckWriter(Writer):
def write(output):
#do something
return 1
d = DuckWriter()
d.write("hello") #Raises TypeError, output must be bytes
d.write(b"hello") #Raises TypeError, return type must be NoneType
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20110403/ea3e00cc/attachment.html>
More information about the Python-ideas
mailing list