我有一个方法,接受一个参数可以是几种类型,并根据类型必须做一件事或其他,但如果我检查所述参数的类型,我不会得到'真正'类型,我总是得到<type 'instance'>
,这正在搞乱我的比较。为什么type(classInstance)返回'instance'?
我有类似:
from classes import Class1
from classes import Class2
# Both classes are declared in the same file.
# I don't know if that can be a problem #
# ... #
def foo(parameter)
if (type(parameter) == type(Class1()):
# ... #
elif (type(parameter) == type(Class2()):
# ... #
而作为type(parameter)
回报<type 'instance'>
和type(Class1())
是<type 'instance'>
为好,事实证明,即使参数是Class2中的一个实例,它是进入第一次比较。 ...
顺便说一下,str(parameter.__class__)
正确显示classes.Class1
。我想我总是可以使用它,但我想知道发生了什么...我做了这样的比较的十分之一和所有他们正常工作...
谢谢! :)
Wooo ...这工程! :) 现在我要试图找出“为什么”...:D – BorrajaX 2010-06-24 14:15:03
它与新风格的类有更多的元数据,包括'type()'触及的东西有关。 – 2010-06-24 14:21:26
Thx再次,我正在阅读: http://www.cafepy.com/article/python_types_and_objects/python_types_and_objects.html 而我种(类)认为这样的事情必须是原因 – BorrajaX 2010-06-24 14:38:45