开发者

Where can I find documentation for calling the various types in Python's types module? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.

We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.

Closed 5 years ago.

Improve this question

While working on some custom serialization, I've been using the types module a lot. One thing that really frustrates me is that the documentation says nothing at all on how to actually call the types. An example: types.CodeType - The type for code objects such as returned by compile(). Yeah sure, what about the 12 arguments it takes??

>>> types.Code开发者_JAVA百科Type()
Traceback (most recent call last):
  File "<string>", line 1, in <fragment>
TypeError: code() takes at least 12 arguments (0 given)

Now, I can generally wrest the information out from various other places in the Python docs, or on the net. For example, I'm guessing new.code(argcount, nlocals, stacksize, flags, codestring, constants, names, varnames, filename, name, firstlineno, lnotab) is the same argument list as expected by types.CodeType (although, an argument list hardly counts as a good documentation).

But seriously, does anyone know documentation that actually describes the possible calls in types?

EDIT: I just noticed one alternative that can be of slight help... help(types.CodeType) etc.


It's generally not very useful to actually instantiate the types in the types module. Their purpose is to be used in isinstance() calls, and some of them can't be instantiated at all (for example types.GeneratorType).

If it's just curiosity, have a look at the documentation available in the interactive interpreter, for example

help(types.CodeType)

If you really think you need to instanciate these types, I would be curious to hear an example use case :)

Edit: Here is a complete categorised list of the types in the types module for Python 2.5 to 2.7. If multiple types are put on the same line, they are just aliases for the same type.

  • 10 types cannot be instantiated at all:

    BuiltinFunctionType, BuiltinMethodType
    DictProxyType
    EllipsisType
    FrameType
    GeneratorType
    GetSetDescriptorType
    MemberDescriptorType
    NoneType
    NotImplementedType
    TracebackType
    
  • 16 types are aliases for built-in names:

    BooleanType                 bool
    BufferType                  buffer
    ComplexType                 complex
    DictType, DictionaryType    dict
    FileType                    file
    FloatType                   float
    IntType                     int
    ListType                    list
    LongType                    long
    ObjectType                  object
    SliceType                   slice
    TupleType                   tuple
    TypeType                    type
    StringType                  str
    UnicodeType                 unicode
    XRangeType                  xrange
    

    To instantiate those, it is preferable to use the built-in name.

  • The remaining 6 types have useful docstrings:

    ClassType
    CodeType
    FunctionType, LambdaType
    InstanceType
    MethodType, UnboundMethodType
    ModuleType
    

    ClassType and InstanceType can be considered obsolete, since they refer to old-style classes.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜