[Python-3000] Adaptation and type declarations

Tim Hochberg tim.hochberg at ieee.org
Mon Apr 10 22:57:14 CEST 2006


Guido van Rossum wrote:
> On 4/10/06, Jim Jewett <jimjjewett at gmail.com> wrote:
> 
>>Every so often Guido talks about adding optional typing to python.
>>
>>Adaptation may offer the cleanest way to do this.
>>
>>
>>Turning
>>
>>    def fn(a, b, c="default"): ...
>>
>>into any of
>>
>>    def fn(Seq a,  Index b, Text c="default"): ...
>>    def fn(Seq(a),  Index(b), Text(c)="default"): ...
>>
>>or (wrapped version)
>>
>>    def fn(Seq a,
>>           Index b,
>>           Text c="default"): ...
>>
>>doesn't seem so awful.  (I'm not sure it is a net positive for
>>readability, but I'm also not sure it isn't.)  I read the type
>>information as "normally this is just an assertion, but I suppose some
>>protocols might *make* it true for me."
> 
> 
> I do think that we now have enough proposals on the table that require
> specifying types (or other metadata!) for each argument of certain
> function definitions that it's time to do something about this.
> 
> A bit more than a year ago I blogged extensively about this. The only
> syntax that is acceptable to me is slightly different; the above would
> look like
> 
> def fn(a: Seq, b: Index, c: Text = "default"): ...
> 
> where Seq, Index and Text can be expressions (the main problem with
> the syntax you propose is that the type can't be much more than an
> identifier before it gets ambiguous or unreadable).
> 
> A completely separate issue is what kind of objects Seq, Index and
> Text would be; but that's a discussion we have separate from the
> syntactic discussion.
> 
> I would imagine that instead of the currently proposed
> 
> @foo.register(list, int, str)
> def foo_1(a, b, c): ...
> 
> we'd be allowed to write
> 
> @foo.register
> def foo_1(a: list, b: int, c: str): ...
> 
> FWIW (to ward of immediate questions) the syntax for an argument would
> be something like this: NAME [':' EXPR] ['=' EXPR] where currently it
> is NAME ['=' EXPR]. The only defined semantics would be that the name,
> the type and the default can all be inspected through the
> __signature__ attribute on the function object.
> 


I think we'd still want some way to explicitly register a function for a 
signature. Either because the function had no types declared or because 
we wanted to register it to different types than the signature implied. 
Say, for example:

@foo.register_for(list, int, unicode)
def foo_2(a, b, c): ...

I imagine register would then be just syntactic sugar for register_for. 
Something like:

def register(obj):
     signature = [x.type for x in obj.__signature__]
     self.register_for(*signature)(obj)

Or something like that.

Regards,

-tim



More information about the Python-3000 mailing list