types.AnyType - describing interfaces with sequences of types

Carlos Alberto Reis Ribeiro cribeiro at mail.inet.com.br
Wed Mar 28 00:22:54 EST 2001


My problem - short question:

I would like to have a type description called AnyType, in such a way that 
it always return true when compared for equality or membership against any 
valid type. Do something like this exist? Does it make sense?

   Example:
   >>> IntType is AnyType
   1
   >>> FloatType == AnyType
   1
   >>> "" = AnyType
   0

Long question:

I was tinkering with some of my proxy experiments, trying a lot of 
different ideas to describe interfaces using only plain Python. One of them 
is the following code snippet:

from types import *

class Interface:
   """ base class for interface specification """
   pass

class InterfaceSequence(Interface):
   """ interface to implement sequence-like objects """
   __len__      = ((), (IntType,))
   __getitem__  = ((IntType,), (NoneType,))
   __setitem__  = ((IntType,NoneType), ())
   __delitem__  = ((IntType,), ())

The idea here is to define some member attributes inside the 
InterfaceSequence class. A helper function then scans 
InterfaceSequence.__dict__ to build the proxy object. Take this only as an 
example, please - the issue here is not the interface, but the types the 
are available in the the 'types' module.

My problem now is: how to describe the method signature? I'm using the 
following data structure in my example:

<method-name>    = (<parameter list>, <return-type>)
<parameter-list> = sequence of type
<return-type>    = type

For example, take the __getitem__ method:

   __getitem__  = ((IntType,), (NoneType,))

This means that __getitem__ have one parameter of IntType (for a sequence, 
remember!), and returns a value of... NoneType (?!?!). This would be much 
better:

   __getitem__  = ((IntType,), (AnyType,))

So that's the problem. I would like to use a value of AnyType for cases 
like this, however, I dont have AnyType as a valid type description. What 
is worse, there is no UserType module to extend type definitions.


p.s. I have some approaches being explored for the interface definition. 
This is one of the alternatives. As for the method signature, there are 
some other alternatives that include more information, such as:

a)   __getitem__  = ((('key', IntType), ), (AnyType,))
b)   __getitem__  = ({'key':IntType}, (AnyType,))
c)   __getitem__  = ({'key':IntType}, AnyType)

The last one seems to be the cleanest. It limits the return value 
description to asingle type, but this seems to be enough for my intentions. 
However, the problem with 'AnyType' is still there.


Carlos Ribeiro






More information about the Python-list mailing list