객체지향프로그래밍_추상화
추상화: 불필요한 세부 사항을 제거하고 가장 본질적이고 공통적인 부분만을 추출하여 객체의 공통속성, 기능을 추출하는 것
예시: def 함수설정
추상화를 잘 하기 위해서
1. 변수, 매소드, 클래스 이름을 잘 짓기
어디에 쓰이는 클래스인지 직관적으로 잘 알아볼 수 있는 이름을 짓는 것이 중요합니다.
2. docstring
하지만 변수, 매소드, 클래스 이름으로만 프로그래밍을 잘 파악하는 것은 무리가 습니다. 이때 사용되는 것이 docstring(문서화)입니다.
클래스가 어떤 목적으로 만들어졌는지, 변수는 어떤 데이터타입이고 어떤 값을 저장하는지, 메소드는 메소드 내에서 변수와 파라미터의 관계를 어떻게 정의하는지 문서화하여 저장하면 더욱 효율적으로 추상화 시킬 수 있습니다.
"""
클래스A: A클래스는 ~한 역할을 합니다.
변수B: 변수B의 데이터 타입은 ~이고 ~을 저장합니다.
매소드C: 매소드C는 ~한 역할을 합니다.
"""
help(클래스A)
위 코드를 사용하면 클래스A에 있는 docstring을 한 눈에 보여줍니다.
help(dict)
class dict(object)
| dict() -> new empty dictionary
| dict(mapping) -> new dictionary initialized from a mapping object's
| (key, value) pairs
| dict(iterable) -> new dictionary initialized as if via:
| d = {}
| for k, v in iterable:
| d[k] = v
| dict(**kwargs) -> new dictionary initialized with the name=value pairs
| in the keyword argument list. For example: dict(one=1, two=2)
|
| Built-in subclasses:
| StgDict
|
| Methods defined here:
|
| __contains__(self, key, /)
| True if the dictionary has the specified key, else False.
|
| __delitem__(self, key, /)
| Delete self[key].
|
| __eq__(self, value, /)
| Return self==value.
|
| __ge__(self, value, /)
| Return self>=value.
|
| __getattribute__(self, name, /)
| Return getattr(self, name).
|
| __getitem__(...)
| x.__getitem__(y) <==> x[y]
|
| __gt__(self, value, /)
| Return self>value.
|
| __init__(self, /, *args, **kwargs)
| Initialize self. See help(type(self)) for accurate signature.
<이하생략>
***reStructuredText(파이썬 공식 문서화 기준)
***Numpy/Scipy
Style guide — numpydoc v1.6.0rc3.dev0 Manual
Style guide This document describes the syntax and best practices for docstrings used with the numpydoc extension for Sphinx. Note For an accompanying example, see example.py. Some features described in this document require a recent version of numpydoc. F
numpydoc.readthedocs.io
3. python의 type hinting
def __init__(self, name:str, deposit:float)->None
동적 타입인 파이썬에서 변수들의 타입을 지정해 줍니다.
정적 타입을 사용하면 입력값이 잘못 입력되었을 때 에러가 나는 것을 쉽게 발견할 수 있습니다. 입력값을 구체화시켜주고 리턴값의 타입도 지정해주면서 프로그래맹을 더욱 구체화시킬 수 있습니다.