Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get list of all Python types (programmatically)?

Tags:

python

types

A discussion in a recent question, 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?

like image 614
Nas Banov Avatar asked Jul 09 '10 06:07

Nas Banov


People also ask

How do I get a list of classes in Python?

Using built-in dir() Function To get the list of all the attributes, methods along with some inherited magic methods of a class, we use a built-in called dir(). Example: Python3.

How do you show data types in Python?

To get the type of a variable in Python, you can use the built-in type() function. In Python, everything is an object. So, when you use the type() function to print the type of the value stored in a variable to the console, it returns the class type of the object.

Can you have a list of different types in Python?

In Python, a list is created by placing elements inside square brackets [] , separated by commas. A list can have any number of items and they may be of different types (integer, float, string, etc.). A list can also have another list as an item. This is called a nested list.

How do I get all the values of a list in Python?

Accessing an element from its index is an easier task in Python, just using the [] operator in a Python list does the trick.


1 Answers

>>> try: import __builtin__ as b
... except ImportError: import builtins as b
...
>>> builtin_types= [t
...  for t in b.__dict__.values()
...  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'>]
like image 56
tzot Avatar answered Sep 18 '22 15:09

tzot