[Python-3000] PEP 3100 Comments

Phillip J. Eby pje at telecommunity.com
Tue May 9 19:56:53 CEST 2006


At 09:57 AM 5/9/2006 -0700, Talin wrote:
>Let me offer the following example as a basis for discussion, and see how 
>it would work in such a system. (People who have different ideas as to how 
>it should be done feel free to chime in -- I want to hear what other 
>techniques are out there.)
>
>Let's define a function called "flatten", which takes a complex data 
>structure and flattens it, in other words, it transforms it into a single 
>linear sequence.
>
>Now, it would be unreasonable to say that "flatten" can handly any 
>arbitrary type that is thrown at it. However, it should be able to handle 
>any container type that is one of the standard "type classes" if I 
>understand what you are saying.
>
>In other words, as long as a type falls into a standard type class, it 
>should be possible for flatten to work with it, even if flatten knows 
>nothing about the specific type, and the type knows nothing about flatten.
>
>How would something like this be done? Is there some way that we can do 
>this in Python?

Assuming we used argument declarations to do overloading, and that actual 
types (other than 'object') are considered to be more specific than 
typeclasses, you would have something like:

     @overloaded
     def flatten(arg:supports(iter)):
         for item in arg:
             for flat in flatten(item):
                 yield flat

     @overloaded
     def flatten(arg:str):
         yield arg

     @overloaded
     def flatten(arg:object):
         yield arg

There are a lot of other ways to spell this, of course; the last two could 
perhaps be combined as a single "def flatten(arg:str|object)", if we had 
union types.

Likewise, it might be that instead of using 'str' as a concrete type, one 
might instead use the existence of some string operation or operations to 
indicate that an object is stringlike.  This has the advantage that any 
stringlike type that supports one of the operations would automatically be 
recognized as string-like.



More information about the Python-3000 mailing list