2019.7.30 헷갈리는 Python의 attribute. (getattr, __getattr__, __getattribute__)
1. getattr(arg) : 객체로부터 arg attribute를 가져오는 함수이다. class Count(object): def __init__(self, mymin, mymax): self.mymin = mymin self.mymax = mymax self.current = None getattr(a, 'mymin') # 10 getattr(a, 'mymax') # 20 getattr(a, 'no attribute') # error raised 2. __getattr__(): 객체의 attribute를 호출했을 때 그 attribute가 존재하지 않는다면 실행되는 함수이다. class Count(object): def __init__(self, mymin, mymax): self.mymin = mymin self.mymax = mymax self.current = None def __getattr__(self, fallback): print("In __getattr__", fallback) a = Count(10, 20) a.mymin # 10 a.mymax # 20 a.noattribute # In __getattr__noattribute 3. __getattribute__ : 객체의 attribute를 조회할 때 조회하는 로직이 실행 되기 전에 실행되는 함수이다. 즉, get을 할 때 항상 도는 코드라고 볼 수 있다. 만약 __getattr__ 과 __getattribute__가 같이 구현되어 있을 때 없는 attribute를 조회한다면 __getattr__의 우선순위가 높다.