저는 항상 파이썬 인터프리터에서 x.__class__
과 type(x)
의 값이 동일하다고 생각했습니다. 그러나 우리는 (또한 PyPy 2.0b1를 파이썬 2.7, 3.3 등) 다음을 수행하는 경우 :weakref가있는 타입의 동작 - 이해할 수 없음
>>> import weakref
>>> x = set()
>>> y = weakref.proxy(x)
>>> x.__class__, isinstance(x, set), type(x)
(<type 'set'>, True, <type 'set'>)
>>> y.__class__, isinstance(y, set), type(y)
(<type 'set'>, True, <type 'weakproxy'>)
우리가 y.__class__
내가 weakref.proxy
그냥 변장에 대한 속성을 대체한다고 가정 (weakref.proxy
을위한 포장 유형에 해당하는 것을 볼 수 있습니다). 심지어 isinstance
은 y
을 set
으로 식별합니다.
그러나 type
은 "true"유형 - weakproxy
을 보여줍니다. 따라서 type
은 인수 유형을 식별하는 데 __class__
속성을 사용하지 않습니다. 그렇습니까? 이를 위해 좀 더 안정적인 소스를 사용합니까? 그렇다면 직접 액세스 할 수 있습니까?
답장을 보내 주셔서 감사합니다. 'isinstance'정보. 먼저 ob_type을 확인한 다음 equals가 아닌 경우 __class__를 검사합니까? –
@IvanYurchenko, 대략, 네, 처음으로'ob_type','__class__'. 사실, 그 사이에는 특별한 훅 ([__instancecheck__]) (http://docs.python.org/2/reference/datamodel.html?highlight=__instancecheck__#class.__instancecheck__)이 있습니다. – nymk
고맙습니다, @nymk. –