views:

67

answers:

2

A discussion in a recent question here (what-is-the-most-pythonic-way-to-have-a-generator-expression-executed) - that min/max() need objects to be comparable - made me wonder how do i find which Python types support particular method (__cmp__, or maybe __lt__ - specifics not important).

Pivotal to that seems to be ability to get list of all types, to start with. Then i can simply check hasattr(thisType, '__cmp__'). So how do i enumerate all data types there are?

+1  A: 

Presence of the relational special methods isn't enough to guarantee comparability; the methods can still raise an exception if they don't like the specific type(s) passed to them.

Ignacio Vazquez-Abrams
Thanks, i already know that (on example of `complex`) - but what i want is a way to enumerate all types, do you know one?
Nas Banov
Well, you can start with `object.__subclasses__()`, but that only catches new-style classes.
Ignacio Vazquez-Abrams
+1  A: 
>>> import __builtin__
>>> builtin_types= [t
...  for t in __builtin__.__dict__.itervalues()
...  if isinstance(t, type)]

>>> import pprint
>>> pprint.pprint(sorted(builtin_types, key=repr))
[<type 'basestring'>,
 <type 'bool'>,
 <type 'buffer'>,
 <type 'bytearray'>,
 <type 'classmethod'>,
 <type 'complex'>,
 <type 'dict'>,
 <type 'enumerate'>,
 <type 'exceptions.ArithmeticError'>,
 <type 'exceptions.AssertionError'>,
 <type 'exceptions.AttributeError'>,
 <type 'exceptions.BaseException'>,
 <type 'exceptions.BufferError'>,
 <type 'exceptions.BytesWarning'>,
 <type 'exceptions.DeprecationWarning'>,
 <type 'exceptions.EOFError'>,
 <type 'exceptions.EnvironmentError'>,
 <type 'exceptions.Exception'>,
 <type 'exceptions.FloatingPointError'>,
 <type 'exceptions.FutureWarning'>,
 <type 'exceptions.GeneratorExit'>,
 <type 'exceptions.IOError'>,
 <type 'exceptions.ImportError'>,
 <type 'exceptions.ImportWarning'>,
 <type 'exceptions.IndentationError'>,
 <type 'exceptions.IndexError'>,
 <type 'exceptions.KeyError'>,
 <type 'exceptions.KeyboardInterrupt'>,
 <type 'exceptions.LookupError'>,
 <type 'exceptions.MemoryError'>,
 <type 'exceptions.NameError'>,
 <type 'exceptions.NotImplementedError'>,
 <type 'exceptions.OSError'>,
 <type 'exceptions.OverflowError'>,
 <type 'exceptions.PendingDeprecationWarning'>,
 <type 'exceptions.ReferenceError'>,
 <type 'exceptions.RuntimeError'>,
 <type 'exceptions.RuntimeWarning'>,
 <type 'exceptions.StandardError'>,
 <type 'exceptions.StopIteration'>,
 <type 'exceptions.SyntaxError'>,
 <type 'exceptions.SyntaxWarning'>,
 <type 'exceptions.SystemError'>,
 <type 'exceptions.SystemExit'>,
 <type 'exceptions.TabError'>,
 <type 'exceptions.TypeError'>,
 <type 'exceptions.UnboundLocalError'>,
 <type 'exceptions.UnicodeDecodeError'>,
 <type 'exceptions.UnicodeEncodeError'>,
 <type 'exceptions.UnicodeError'>,
 <type 'exceptions.UnicodeTranslateError'>,
 <type 'exceptions.UnicodeWarning'>,
 <type 'exceptions.UserWarning'>,
 <type 'exceptions.ValueError'>,
 <type 'exceptions.Warning'>,
 <type 'exceptions.ZeroDivisionError'>,
 <type 'file'>,
 <type 'float'>,
 <type 'frozenset'>,
 <type 'int'>,
 <type 'list'>,
 <type 'long'>,
 <type 'object'>,
 <type 'property'>,
 <type 'reversed'>,
 <type 'set'>,
 <type 'slice'>,
 <type 'staticmethod'>,
 <type 'str'>,
 <type 'str'>,
 <type 'super'>,
 <type 'tuple'>,
 <type 'type'>,
 <type 'unicode'>,
 <type 'xrange'>]
ΤΖΩΤΖΙΟΥ