본문 바로가기

imports2

Python 모듈의 import 문은 항상 맨 위에 있어야 합니까?, Should import statements always be at the top of a module? 질문 PEP 8는 다음과 같이 명시합니다: 임포트는 항상 파일의 맨 위에 위치하며, 모듈 주석과 독스트링 바로 다음에 오고, 모듈 전역 변수와 상수 앞에 위치합니다. 하지만 내가 임포트하는 클래스/메소드/함수가 드물게 사용되는 경우, 필요할 때만 임포트하는 것이 더 효율적이지 않을까요? 이렇게 하는 것이: class SomeClass(object): def not_often_called(self) from datetime import datetime self.datetime = datetime.now() 이렇게 하는 것보다 더 효율적이지 않을까요? from datetime import datetime class SomeClass(object): def not_often_called(self) self.d.. 2023. 10. 9.
Python PyCharm에서 미해결된 참조 문제, Unresolved reference issue in PyCharm 질문 저는 디렉토리 구조를 가지고 있습니다. ├── simulate.py ├── src │ ├── networkAlgorithm.py │ ├── ... 그리고 sys.path.insert()를 사용하여 네트워크 모듈에 액세스할 수 있습니다. import sys import os.path sys.path.insert(0, "./src") from networkAlgorithm import * 하지만, pycharm은 해당 모듈에 액세스할 수 없다고 불평합니다. pycharm에게 참조를 해결하는 방법을 가르칠 수 있을까요? 답변 Manually adding it as you have done is indeed one way of doing this, but there is a simpler method, a.. 2023. 10. 7.