简体   繁体   English

为什么type(classInstance)返回'instance'?

[英]Why type(classInstance) is returning 'instance'?

I have a method that accepts a parameter that can be of several types, and has to do one thing or other depending on the type, but if I check the type of said parameter, I don't get the 'real' type, I always get <type 'instance'> , and that is messing up with my comparisons. 我有一个方法接受一个可以是几种类型的参数,并且必须根据类型做一件事或者其他事情,但如果我检查所述参数的类型,我不会得到'真实'类型,我总是得到<type 'instance'> ,这就搞砸了我的比较。

I have something like: 我有类似的东西:

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()):
    # ... #

And as type(parameter) returns <type 'instance'> and type(Class1()) is <type 'instance'> as well, it turns out that even if the parameter is an instance of Class2, it is going into the first comparison... 并且当type(parameter)返回<type 'instance'> type(Class1()) <type 'instance'>并且type(Class1())也是<type 'instance'> type(Class1()) <type 'instance'> ,事实证明即使参数是Class2的一个实例,它也会进入第一个比较...

By the way, str(parameter.__class__) properly shows classes.Class1 . 顺便说一下, str(parameter.__class__)正确地显示了classes.Class1 I guess I could always use that, but I would like to understand what's going on... I have made tenths of comparisons like this and all them worked properly... 我想我总是可以使用它,但我想了解发生了什么...我已经做了十分之一的比较,所有这些都正常工作......

Thank you!! 谢谢!! :) :)

Old-style classes do that. 旧式课程就是这么做的。 Derive your classes from object in their definitions. 从定义中的object派生类。

you should really use isinstance: 你应该真的使用isinstance:

In [26]: def foo(param):
   ....:     print type(param)
   ....:     print isinstance(param, Class1)
   ....:

In [27]: foo(x)
<type 'instance'>
True

Type is better for built-in types. 对于内置类型,类型更好。

The fact that type(x) returns the same type object for all instances x of legacy, aka old-style, classes, is one of many infuriating defects of those kinds of classes -- unfortunately they have to stay (and be the default for a class without base) in Python 2.* for reasons of backwards compatibility. type(x)为遗留的所有实例x返回相同类型对象的事实,即旧式类,是这类类的许多令人愤怒的缺陷之一 - 不幸的是它们必须保留(并且是Python 2.*一个没有基类的类2.*出于向后兼容的原因。

Nevertheless, don't use old-style classes unless you're forced to maintain a bunch of old, legacy code (without a good test suite to give you the confidence to try and switch kind o classes). 不过, 不要使用旧式的类,除非你被迫维护一堆旧的遗留代码(没有一个好的测试套件让你有信心尝试切换类o)。 When a class has no "natural" bases, subclass it from object rather than from nothing. 当一个类没有“自然”基础时,将它从object继承而不是从无。 Alternatively, your module, at the top, can set 或者,您的模块位于顶部,可以设置

__metaclass__ = type

which changes the default from the crufty, legacy old-style classes, to the shiny bright new-style ones -- while explicitly inheriting from object is usually preferred ("explicit is better than implicit"), the module-global setting of __metaclass__ may feel "less invasive" to existing old modules where you're switching from old to new classes, so it's offered as a possibility. 它将默认从古老的旧式类更改为闪亮的新式类 - 虽然显式继承自object通常是首选(“显式优于隐式”), __metaclass__的模块全局设置可能对现有的旧模块感觉“侵入性较小”,你可以从旧课程转换到新课程,因此可以提供。

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

相关问题 类方法返回实例的类型注释 - Type annotation for classmethod returning instance 为什么`object`是`type`的实例而`type`是`object`的实例? - Why is `object` an instance of `type` and `type` an instance of `object`? 为什么python中的对象是&#39;instance&#39;类型? - Why an object in python is of type 'instance'? 为什么`type(myField)`返回`<type 'instance'> `而不是`<type 'field'> `?</type></type> - Why does `type(myField)` return `<type 'instance'>` and not `<type 'Field'>`? 为什么一个额外的无类型 str 正在返回? - Why an extra none type str is returning? 为什么类型函数为字典返回 False? - Why is the type function returning False for a dictionary? Biopython Phylo.draw():为什么不返回轴实例? - Biopython Phylo.draw() : Why not returning axes instance? Requests.get(&#39;url&#39;)返回TypeError super(type,obj):obj必须是类型的实例或子类型 - Requests.get('url') is returning TypeError super(type, obj): obj must be an instance or subtype of type 为什么实例类型的价格显示与原始AWS列表不同? - Why price of an instance type showing different from original AWS list? 为什么它返回 TypeError:“NoneType”类型的 object 没有 len() - Why is it returning TypeError: object of type 'NoneType' has no len()
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM