본문 바로가기
CS 지식 정리/개발기록

[python] hasattr(object, attribute) object에 attribute 속성이 있는지 참/거짓 여부

by Alan_Kim 2023. 11. 12.
728x90
반응형

 

hasattr(object, attribute)는 object에 attribute 속성이 있으면 True, 없으면 False를 출력하는 함수를 이야기한다.

 

class example:
    def __init__(self, x):
        self.x =x 

c = example(1)
print(c.x) # 1
print(hasattr(c, 'x')) # True
print(hasattr(c, 'y')) # False

 

주로 DL에서 속성이 있을 때 초기화 하는 용도로 사용한다.

728x90
반응형

댓글