variables - Where is a list of all of python's `__builtin__` datatypes? -


i'm using comparisons like:

if type( self.__dict__[ key ] ) str \     or type( self.__dict__[ key ] ) set \     or type( self.__dict__[ key ] ) dict \     or type( self.__dict__[ key ] ) list \     or type( self.__dict__[ key ] ) tuple \     or type( self.__dict__[ key ] ) int \     or type( self.__dict__[ key ] ) float: 

i've once discovered, i've missed bool type:

or type( self.__dict__[ key ] ) bool \,

okay - wondered other types missed?

i've started googling:

  • diveintopython3:

    python has many native datatypes. here important ones:

    1. booleans either true or false.
    2. numbers can integers (1 , 2), floats (1.1 , 1.2), fractions (1/2 , 2/3), or complex numbers.
    3. strings sequences of unicode characters, e.g. html document.
    4. bytes , byte arrays, e.g. jpeg image file.
    5. lists ordered sequences of values.
    6. tuples ordered, immutable sequences of values.
    7. sets unordered bags of values.
    8. dictionaries unordered bags of key-value pairs.

why everywhere people talking many types, can't find list of of them? it's important ones

you can iterate on __builtin__'s __dict__, , use isinstance see if class:

builtins = [e (name, e) in __builtin__.__dict__.items() if isinstance(e, type) , e not object] >>> builtins [bytearray,  indexerror,  syntaxerror,  unicode,  unicodedecodeerror,  memoryview,  nameerror,  byteswarning,  dict'  systemexit  ... 

(note @user2357112 pointed out in excellent comment, explicitly excluding object, not useful.)

note isinstance can take tuple second argument, can use instead of series of ifs. consequently, can write things so:

builtins = tuple([e (name, e) in __builtin__.__dict__.items() if isinstance(e, type) , not isinstance(object, e)]) >>> isinstance({}, builtin_types) true 

Comments

Popular posts from this blog

Spring Boot + JPA + Hibernate: Unable to locate persister -

go - Golang: panic: runtime error: invalid memory address or nil pointer dereference using bufio.Scanner -

c - double free or corruption (fasttop) -