isinstance(xxx, 属性) 有效,但是它在类型模块中对应什么?
isinstance(xxx, property) works, but what does that correspond to in the types module?
我可以使用 isinstance 来测试属性是否为 属性(实际上在 class 上),但我在对应于 属性.
的 types 模块
希望下面的代码能让您更清楚地了解这一点。我正在测试的所有其他东西都能很好地匹配。
class MyClass(object):
def __init__(self):
self._counter = 0
def counter():
doc = "The counter property."
def fget(self):
self._counter +=1
return self._counter
return locals()
counter = property(**counter())
def bar(self):
self
foo = MyClass()
print "foo.counter", foo.counter
print "foo.counter", foo.counter
print "this will be an int. type(foo.counter):", type(foo.counter)
print "but the class knows it's a property isinstance(foo.__class__.counter, property):", isinstance(foo.__class__.counter, property)
#let's see if we can determine the type of a given item...
import types
to_test = [3, foo.__class__.counter, foo.bar, {}]
print "matches against types:"
for k, v in vars(types).items():
for test in to_test[:]:
if type(test) == v:
#flag a match, remove it from tests
print (" %s is a %s" % (test, v))
to_test.remove(test)
#what's left over? the property
print "\n\nno match found... :(", to_test
输出...
foo.counter 1
foo.counter 2
this will be an int. type(foo.counter): <type 'int'>
but the class knows its a property isinstance(foo.__class__.counter, property): True
matches against types:
3 is a <type 'int'>
<bound method MyClass.bar of <__main__.MyClass object at 0x106b06a50>> is a <type 'instancemethod'>
{} is a <type 'dict'>
no match found... :( [<property object at 0x106afc838>]
什么给了?为什么 属性 在任何地方都无法识别类型?
我很好奇的原因是我有一个调试功能,试图漂亮地打印复杂的对象。我很久以前就知道在这些对象上调用方法不是一个好主意。所以我传入一个默认的属性类型列表不看。
li_skiptype=[types.MethodType]
几乎所有的时间,我都会跳过方法,但如果我从这个列表中删除它们,我可以很好地打印它们。最好添加 并使我也有条件地跳过 属性 类型。
好吧...我只是很好奇为什么它们不是类型。
types
并不意味着是所有 Python 对象类型的综合集合。来自 module documentation:
This module defines names for some object types that are used by the standard Python interpreter, but not for the types defined by various extension modules.
强调我的。
模块中的大多数对象只是内置类型的别名,参见 module source code;这里的类型对象没有任何神奇之处;它们只是更多的 Python 对象,其中一些不容易暴露。该模块只是一个方便。
您已经有了要测试的 property
对象,您也不需要在 types
模块中引用该对象。
在 Python 3 中,types
模块进一步缩减为一组有限的类型,否则可能很难获得引用。已删除的类型是那些可以作为内置函数直接使用的类型。再次引用 Python 2 文档:
Starting in Python 2.2, built-in factory functions such as int()
and str()
are also names for the corresponding types. This is now the preferred way to access the type instead of using the types module.
由于 property
从一开始就作为内置语言添加到语言中,因此无需在 types
中公开它,因为您已经可以直接访问它。
请注意,使用 isinstance()
几乎总是更好,并允许子类。如果您必须 将测试限制为仅一种类型,请使用is
作为单例类型;使用 type(obj) is sometype
而不是 type(obj) == sometype
。另见 Python styleguide:
Object type comparisons should always use isinstance()
instead of comparing types directly.
我可以使用 isinstance 来测试属性是否为 属性(实际上在 class 上),但我在对应于 属性.
的 types 模块希望下面的代码能让您更清楚地了解这一点。我正在测试的所有其他东西都能很好地匹配。
class MyClass(object):
def __init__(self):
self._counter = 0
def counter():
doc = "The counter property."
def fget(self):
self._counter +=1
return self._counter
return locals()
counter = property(**counter())
def bar(self):
self
foo = MyClass()
print "foo.counter", foo.counter
print "foo.counter", foo.counter
print "this will be an int. type(foo.counter):", type(foo.counter)
print "but the class knows it's a property isinstance(foo.__class__.counter, property):", isinstance(foo.__class__.counter, property)
#let's see if we can determine the type of a given item...
import types
to_test = [3, foo.__class__.counter, foo.bar, {}]
print "matches against types:"
for k, v in vars(types).items():
for test in to_test[:]:
if type(test) == v:
#flag a match, remove it from tests
print (" %s is a %s" % (test, v))
to_test.remove(test)
#what's left over? the property
print "\n\nno match found... :(", to_test
输出...
foo.counter 1
foo.counter 2
this will be an int. type(foo.counter): <type 'int'>
but the class knows its a property isinstance(foo.__class__.counter, property): True
matches against types:
3 is a <type 'int'>
<bound method MyClass.bar of <__main__.MyClass object at 0x106b06a50>> is a <type 'instancemethod'>
{} is a <type 'dict'>
no match found... :( [<property object at 0x106afc838>]
什么给了?为什么 属性 在任何地方都无法识别类型?
我很好奇的原因是我有一个调试功能,试图漂亮地打印复杂的对象。我很久以前就知道在这些对象上调用方法不是一个好主意。所以我传入一个默认的属性类型列表不看。
li_skiptype=[types.MethodType]
几乎所有的时间,我都会跳过方法,但如果我从这个列表中删除它们,我可以很好地打印它们。最好添加 并使我也有条件地跳过 属性 类型。
好吧...我只是很好奇为什么它们不是类型。
types
并不意味着是所有 Python 对象类型的综合集合。来自 module documentation:
This module defines names for some object types that are used by the standard Python interpreter, but not for the types defined by various extension modules.
强调我的。
模块中的大多数对象只是内置类型的别名,参见 module source code;这里的类型对象没有任何神奇之处;它们只是更多的 Python 对象,其中一些不容易暴露。该模块只是一个方便。
您已经有了要测试的 property
对象,您也不需要在 types
模块中引用该对象。
在 Python 3 中,types
模块进一步缩减为一组有限的类型,否则可能很难获得引用。已删除的类型是那些可以作为内置函数直接使用的类型。再次引用 Python 2 文档:
Starting in Python 2.2, built-in factory functions such as
int()
andstr()
are also names for the corresponding types. This is now the preferred way to access the type instead of using the types module.
由于 property
从一开始就作为内置语言添加到语言中,因此无需在 types
中公开它,因为您已经可以直接访问它。
请注意,使用 isinstance()
几乎总是更好,并允许子类。如果您必须 将测试限制为仅一种类型,请使用is
作为单例类型;使用 type(obj) is sometype
而不是 type(obj) == sometype
。另见 Python styleguide:
Object type comparisons should always use
isinstance()
instead of comparing types directly.