[英]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
即
file
, filename
, content_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.