-
[Python] hasattr()언어/파이썬 & 장고 2016. 10. 29. 14:56
hasattr(object, name)
Object내에 name에 해당하는 attribute가 있으면 True, 없으면 False를 리턴.
class foobar(): data = [1, 2, 3, 4] def __init__(self, val): self.val = val >>> x = foobar >>> y = foobar(['a', 'b']) >>> z = foobar([1, 2]) >>> hasattr(x, 'data') True >>> hasattr(y, 'data') True >>> hasattr(x, 'val') False >>> hasattr(y, 'val') True >>> delattr(x, 'data') >>> hasattr(x, 'data') False >>> hasattr(y, 'data') False >>> hasattr(z, 'val') True >>> delattr(z, 'val') >>> hasattr(z, 'val') False >>> hasattr(y, 'val') True