Notice
Recent Posts
Recent Comments
Link
250x250
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | 7 |
8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 27 | 28 |
29 | 30 |
Tags
- 모바일
- Python
- kafka
- numpartitions
- query history
- spark
- freshness
- Materializations
- 윈도우
- 파이썬
- proerty
- dbt_project
- k9s
- 쿠버네티스
- UI for kafka
- mysql
- Java
- 동적 차트
- polars
- airflow
- KubernetesPodOperator
- DBT
- ksql
- spring boot
- bar cahrt
- 도커
- 카프카
- CDC
- docker
- 크롤링
Archives
- Today
- Total
데이터 엔지니어 이것저것
파이썬 클린코드 (밑줄 _ 의 사용) 본문
728x90
파이썬에서 밑줄( _ ) 을 사용하는 몇 가지 규칙과 구현 세부 사항이 있다.
python 은 기본적으로 객체의 모든 속성은 public 이다.
class Connector:
def __init__(self, source):
self.source = source
self._timeout = 60
conn = Connector("postgresql://localhost")
conn.source
#'postgresql://localhost'
conn._timeout
#60
conn.__dict__
#{'source': 'postgresql://localhost', '_timeout': 60}
source의 경우 public, _timeout의 경우 private이다.
하지만 실제론 두 개의 속성에 모두 접근 가능하다
( __ ) 밑줄 2개로 정의를 해보자
class Connector:
def __init__(self, source):
self.source = source
self.__timeout = 60
def connect(self):
print('connecting with {0}s'.format(slef.__timeout))
conn = Connector("postgresql://localhost")
conn.connect()
#connecting with 60s
conn.__timeout()
'''
error
AttributeError Traceback (most recent call last)
<ipython-input-13-bbc8b27e4d99> in <module>
----> 1 conn.__timeout()
AttributeError: 'Connector' object has no attribute '__timeout'
'''
에러를 보면 '속성이 존재하지 않는다' 이다.
private 나 접근 할 수 없다가 아닌 존재하지 않는다 이다.
이것을 접근할수없다라고 인식하면 생기는 문제이다
python은 밑줄 두개를 사용하면 다른 이름을 만든다.
이를 맹글링(name mangling)이라 한다. 이름의 속성을 만드는것이다.
문법 : "_(class-name)__(attribute-name)"
vars(conn)
#{'source': 'postgresql://localhost', '_Connector__timeout': 60}
conn._Connector__timeout
#60
conn._Connector__timeout = 30
conn.connect()
#connecting with 30s
python을 사용할떄 __ 을 사용하는건 python 스럽지 않다. private를 정의하기 위해선 _ 만 사용하자
다시 말하지만 _를 사용하면 private가 아닌 private로 사용하자는 약속같은것이다.
728x90
'개발언어 > Python' 카테고리의 다른 글
request로 File 보내기 (0) | 2020.12.20 |
---|---|
윈도우 알림 기능 (0) | 2020.12.19 |
계약에 의한 디자인 (Design by Contract) (0) | 2020.11.08 |
슬랙 api (0) | 2020.11.02 |
python) API를 활용한 티스토리 업로드 (0) | 2020.09.09 |