types.AnyType - describing interfaces with sequences of types

Ixokai ixokai-nf at myNOSPAM.org
Wed Mar 28 05:56:08 EST 2001


Try this:

class _AnyType:
    def __cmp__(self, other):
        return 0
    def __rcmp(self, other):
        return 0

AnyType = _AnyType()

>>> IntType == AnyType
1
>>> type("string") == AnyType
1

You get the idea. AnyType above will be eternally happy to be equivelent to
anything you compare it to. Note that you can't do 'IntType is AnyType',
that tells you actual object identity. Now, if you wanted it to only show up
as 'equivelent' to TypeTypes, and not say, strings, (e.g. "string" ==
AnyType) you could do a check to make sure what its being compared to is a
TypeType.....

Its a hack, yes.. :)

--Ixokai
( Replace 'NOSPAM with 'seraph' to respond)

Carlos Alberto Reis Ribeiro wrote in message ...
>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
>
>
>




-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----==  Over 80,000 Newsgroups - 16 Different Servers! =-----



More information about the Python-list mailing list