본문 바로가기

Class8

Python 왜 "TypeError: Missing 1 required positional argument: 'self'"가 발생하나요?, Why do I get "TypeError: Missing 1 required positional argument: 'self'"? 질문 저는 다음과 같은 코드를 가지고 있습니다: class Pump: def __init__(self): print("init") def getPumps(self): pass p = Pump.getPumps() print(p) 하지만 다음과 같은 오류가 발생합니다: Traceback (most recent call last): File "C:\Users\Dom\Desktop\test\test.py", line 7, in p = Pump.getPumps() TypeError: getPumps() missing 1 required positional argument: 'self' 왜 __init__이 호출되지 않는 것 같고, 이 예외는 무엇을 의미합니까? 제 이해에 따르면, self가 생성자와 메소드에 자동.. 2023. 12. 7.
Python 왜 AttributeError: 'NoneType' 개체에는 'something' 속성이 없다고 나오나요?, Why do I get AttributeError: 'NoneType' object has no attribute 'something'? 질문 에러 메시지를 받고 있습니다. AttributeError: 'NoneType' 개체에는 'something' 속성이 없습니다. 이 메시지를 어떻게 이해할 수 있을까요? 어떤 일반적인 시나리오가 이러한 AttributeError를 발생시킬 수 있으며, 문제를 어떻게 식별할 수 있을까요? 이것은 AttributeError의 특수한 경우입니다. 코드에서 예기치 않은 None 값을 얻는 방법이 많기 때문에 일반적으로 다른 문제입니다. 다른 AttributeError의 경우, 문제는 속성 이름일 수도 있습니다. None 값이란 무엇인가요?와 'NoneType' 개체란 무엇인가요?도 None과 그 유형인 NoneType을 이해하는 데 도움이 됩니다. 답변 NoneType은 일반적으로 여러분이 작업 중인 클래스.. 2023. 11. 16.
Python 메모이제이션은 무엇이며, 파이썬에서 어떻게 사용할 수 있을까요?, What is memoization and how can I use it in Python? 질문 저는 방금 Python을 시작했고 memoization이 무엇이며 어떻게 사용하는지 전혀 모르겠습니다. 또한, 간단한 예제를 볼 수 있을까요? 답변 메모이제이션은 메소드 호출의 결과를 메소드의 입력에 기반하여 기억하고, 결과를 다시 계산하는 대신 기억된 결과를 반환하는 것을 효과적으로 의미합니다. 이것을 메소드 결과의 캐시로 생각할 수 있습니다. 자세한 내용은 Introduction To Algorithms (3e), Cormen et al.의 387페이지를 참조하십시오. 파이썬에서 메모이제이션을 사용하여 팩토리얼을 계산하는 간단한 예제는 다음과 같습니다: factorial_memo = {} def factorial(k): if k < 2: return 1 if k not in factorial_m.. 2023. 10. 24.
Python 싱글톤을 정의하는 간단하고 우아한 방법이 있을까요? [중복됨], Is there a simple, elegant way to define singletons? [duplicate] 질문 파이썬에서 싱글톤을 정의하는 다양한 방법이 있는 것 같습니다. Stack Overflow에서는 합의된 의견이 있을까요? 답변 I don't really see the need, as a module with functions (and not a class) would serve well as a singleton. All its variables would be bound to the module, which could not be instantiated repeatedly anyway. If you do wish to use a class, there is no way of creating private classes or private constructors in Python, so you ca.. 2023. 10. 12.