https://goodthings4me.tistory.com/60
class Simple:
def __init__(self, name):
self.name = name
def __str__(self):
return '이름은 %s 입니다.' % self.name
def main():
sp = Simple('Kim')
print(sp)
sp.__dict__['name'] = 'Park'
print(sp)
main()
-참고로, 밑줄 두 개를 붙이면 외부 접근이 불가한 private 효과를 만들 수는 있지만, 실제로 private은 아니다.
- 이는 Name Mangling(내부적인 어떤 규칙을 통해 함수의 이름을 바꾸는 것)으로 불리는 것으로, 클래스의 확장 시 메서드의 충돌없는 오버라이드를 위해 만들어진 것이라고 한다.
- 이를 private 효과를 본다고 생각하면 안되며, 일부 오류(부작용)를 초래할 수 있기 때문에 파이썬에서는 객체의 인터페이스 용도(접근 용도)가 아닌 속성(또는 메서드)에는 밑줄(_) 두 개가 아닌 하나를 붙여주는 것이 좋다.
이는 외부에서 보호된 이름으로 사용되기에 호출해서 사용하면 안된다; 클래스를 사용할 때 내부적으로만 사용할 속성이나 메서드에 붙힌다.
class Protected:
def __init__(self, name, age):
self._set(name, age)
def _set(self, name, age):
self._name = name
self._age = age
def getname(self):
return self._name
def getage(self):
return self._age
p = Protected('로버트', 30)
print(p.__dict__)
>>> {'_name': '로버트', '_age': 30}
print(p.getname())
print(p.getage())
>>> 로버트
>>> 30
# 다 공개가 되어있기 때문에 속성의 이름을 알면 바로 접근해서 조회할 수 있다.
print(p._name)
print(p._age)
>>> 로버트
>>> 30
class Mangling:
def __init__(self, name, age):
self.__set(name, age)
def __set(self, name, age):
self.__name = name
self.__age = age
def getname(self):
return self.__name
def getage(self):
return self.__age
p = Mangling('로버트', 31)
print(p.__dict__)
>>> {'_Mangling__name':'로버트', 'Mangling__age':31}
print(p.getname())
print(p.getage())
>>>로버트
>>>31
print(p._name)
print(p._age)
>>>error
class PropertyClass:
def __init__(self, name):
self._name = name
# 조회
@property
def name(self):
return self._name
# 갱신
@name.setter
def name(self, value):
self._name = value
p = PropertyClass('설린저')
print(p.name)
>>>설린저
# 조회하면 변경된 것을 확인 가능
p.name = '제라드'
print(p.name)
>>>제라드
print(p.__dict__)
print(p.name)
p._name = '로버트'
print(p.name)
>>> {'_name':'제라드'}
>>> 제라드
>>> 로버트
pprint(): pretty print
>>> import pprint
>>> result = {'userId': 1, 'id': 1, 'title': 'sunt aut facere repellat provident occaecati excepturi optio reprehenderit', 'body': 'quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto'}
>>> pprint.pprint(result)
{'body': 'quia et suscipit\n'
'suscipit recusandae consequuntur expedita et cum\n'
'reprehenderit molestiae ut ut quas totam\n'
'nostrum rerum est autem sunt rem eveniet architecto',
'id': 1,
'title': 'sunt aut facere repellat provident occaecati excepturi optio '
'reprehenderit',
'userId': 1}
구조가 복잡한 JSON데이터 디버깅 용도로도 사용 가능하다.
참고문헌)
https://goodthings4me.tistory.com/60
https://sikaleo.tistory.com/117