[Python-3000] Draft pre-PEP: function annotations (original) (raw)

Phillip J. Eby pje at telecommunity.com
Tue Aug 15 03:37:47 CEST 2006


At 01:19 PM 8/15/2006 +1200, Greg Ewing wrote:

Phillip J. Eby wrote:

> The examples there are very short and simple; in fact the complete Message implementation, including imports and overload declarations is only 6 lines long. So, my only guess is that the people who looked at that skimmed right past it, looking for something more complicated! If it really is that short and simple, why not just post the whole thing? Then there's no danger of anyone getting lost in parts of the documentation they're not supposed to be looking at.

Here are the most relevant bits excerpted from the text:

To create a new kind of metadata, we need to create a class that represents the metadata, and then add a method to the binding.declareAttribute() generic function. For our example, we'll create a Message metadata type that just prints a message when the metadata is registered::

 >>> class Message(str):
 ...     pass

 >>> def print_message(classobj, attrname, metadata):
 ...     print metadata, "(%s.%s)" % (classobj.__name__, attrname)

 >>> binding.declareAttribute.addMethod(Message,print_message)

Now, we'll see if it works::

 >>> class Foo: pass
 >>> binding.declareAttribute(Foo, 'bar', Message("testing"))
 testing (Foo.bar)

In addition to defining your own metadata types, declareAttribute() has built-in semantics for None and sequence types. The former is a no-op, and the latter re-invokes declareAttribute() on the sequence contents::

 >>> binding.declareAttribute(Foo, 'baz',
 ...     [Message('test1'), Message('test2')]
 ... )
 test1 (Foo.baz)
 test2 (Foo.baz)

 >>> binding.declareAttribute(Foo, 'spam', None)     # no-op


More information about the Python-3000 mailing list