简体   繁体   English

如何确定在python中传递的参数的类型

[英]How can I determine the type of an argument passed in python

Specifically I want to know which properties are available on myFile in the following code sample: 具体来说,我想知道以下代码示例中myFile上可用的属性:

def upload(self, myFile):
    out = """<html>
    <body>
        myFile length: %s<br />
        myFile filename: %s<br />
        myFile mime-type: %s
    </body>
    </html>"""

    # Although this just counts the file length, it demonstrates
    # how to read large files in chunks instead of all at once.
    # CherryPy reads the uploaded file into a temporary file;
    # myFile.file.read reads from that.
    size = 0
    while True:
        data = myFile.file.read(8192)
        if not data:
            break
        size += len(data)

    return out % (size, myFile.filename, myFile.content_type)
upload.exposed = True

This is taken from the CherryPy file upload example, and it shows a couple of the properties available in the documentation. 这取自CherryPy文件上载示例,并显示了文档中可用的几个属性。 Namely file , filename , content_type filefilenamecontent_type

But how can I determine all the properties, or better what the actual type is so I can open the source and read the properties? 但是,如何确定所有属性,或者更好地确定实际类型是什么,以便我可以打开源并读取属性?

The type can be obtained with type(myFile) . 可以使用type(myFile)获得type(myFile) You can use inspect module or myFile.__dict__ to see properties. 您可以使用inspect模块或myFile.__dict__来查看属性。

If you want to see the source code, use type(myFile).__module__ to see where it is defined. 如果要查看源代码,请使用type(myFile).__module__查看其定义位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何确定Python类型错误的原因 - How can I determine the reason for a Python Type Error 如何在Python中确定常见图像类型的类型和大小? - How can I determine type and size of common image types in Python? 我如何确定python使用的类型的确切大小 - How can i determine the exact size of a type used by python Python函数如何确定是否(以及如何)显式传递了kwarg默认值? - How can a Python function determine if (and how) a kwarg default was explicitly passed? 如何在 Python 中检查传递的 CLI 参数类型/单击 - How to check the passed CLI argument type in Python / Click Python:如何检查参数的类型是按值还是按引用传递? - Python: how to check if an argument's type is such that it will be passed by value or by reference? 如果没有在+ python中传递参数,我该如何分配默认值 - how do i assign a default value if no argument is passed in + python 如何获取传递给 Python 装饰器的参数的值? - How do I get the value of an argument passed to a decorator in Python? Python:确定作为函数参数传递的列表中的元素数 - Python: determine number of elements in a list passed as a function argument 仅当传递的参数为字符串时,才能实例化类吗? - How can I instantiate class only if argument passed is string?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM