[Python-Dev] Py3: function signatures, type checking, and related crap (original) (raw)
SevenInchBread adamadamadamamiadam at gmail.com
Sun Apr 15 23:49:58 CEST 2007
- Previous message: [Python-Dev] functools additions
- Next message: [Python-Dev] Python 2.5.1c1 pickle problem
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
People seem to be pushing for a consistent method for checking the "x-ness" of objects (that is, interfaces that the object implements).
So I present an idea for a simple and straightforward type that provides a way to construct descriptions of object structures, and I'd like some help expanding it into a useful extension to Python's standard set of utilities.
Its a basic constructor that produces callable interface-checking predicates (which can be use in things such as list comprehensions, filter, if statements, or even a new syntax for function signatures that allows for automatic interface-checking). These predicates check that an object matches the behavior described by the constructor . Since I can't think of a name for this constructor, and because I've never liked the term "interface", I'll just call it "can".
Can takes an arbitrary number of keyword arguments and produces a callable object. The keys represent object attributes, while the values are behavior-checking predicates like the ones produced by can. Since the can constructor produces an object that can in turn be used in other can constructors, using previously defined interfaces in new constructions is fairly straight-forward
callable = can(__call__ = object)#Returns an object that describes
objects with a call attribute readable = can(read = callable)
...with a callable read attribute
writable = can(write = callable)
...with a callable write attribute
#a join operator can be used to combine can objects...
#...for now I'll just use "and" and "or" to represent them.
isfilelike = readable and writable #returns an object that matches any
type that is described #by both readable and writable
IOable = readable or writable #any type that is readable or
writable
objects that are constructed with can, when called, return True or False based on whether or not the passed object matches the behavior described.
callable(hash) #returns True - as it would in the current version
of Python.
Here's some more nifty examples:
iterable = can(__iter__=callable) or can(next=callable)
hashable = can(__hash__=callable)
completefile = isfilelike and iterable and can(fileno=callable,
close=callable)
def outlines(f, seq):
"""Outputs a sequence of lines to a file-like object"""
assert isfilelike(f), "%r is not a file-like object." % f
assert isiterable(seq), "%r must be iterable" % seq
f.write("\n".join(seq))
#a trivial example... you'd get similar error messages from Python
builtins even without the assertions.
As it stands, I don't think that this deserves to be in Python - but I think the basic premise could be used as a foundation for better things.
-- "What's money? A man is a success if he gets up in the morning and goes to bed at night and in between does what he wants to do." ~ Bob Dylan -------------- next part -------------- An HTML attachment was scrubbed... URL: http://mail.python.org/pipermail/python-dev/attachments/20070415/fbece2de/attachment.htm
- Previous message: [Python-Dev] functools additions
- Next message: [Python-Dev] Python 2.5.1c1 pickle problem
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]