인스턴스 객체가 어떤 클래스로부터 생성됐는지 확인하는 방법
isinstance(인스턴스 객체, 클래스 객체)
>>> class Person:
pass
>>> class Bird:
pass
>>> class Student(Person):
pass
>>> p, s = Person(), Student()
>>> print("p is instance of Person: ", isinstance(p, Person))
p is instance of Person: True
>>> print("p is instance of Person: ", isinstance(s, Person)) <<상속관계 판단 가능
p is instance of Person: True
>>> print("p is instance of Person: ", isinstance(p, object)) <<버전 3이후 모든 클래스는 object에서 파생
p is instance of Person: True
>>> print("p is instance of Person: ", isinstance(p, Bird))
p is instance of Person: False
>>> print("int is instance of Person: ", isinstance(int, object)) <<기본 자료형도 object에서 파생
int is instance of Person: True
'컴퓨터 > Python' 카테고리의 다른 글
정적메서드, 클래스메서드 (0) | 2013.07.17 |
---|---|
생성자, 소멸자 메서드 (0) | 2013.07.16 |
클래스 객체 와 인스턴스 객체의 이름공간 (0) | 2013.07.15 |
클래스 선언 (0) | 2013.07.15 |
제어문과 연관된 유용한 함수 (0) | 2013.07.12 |