본문 바로가기

Python Object-oriented Programming49

Custom Exceptions https://www.pythontutorial.net/python-oop/python-custom-exception/ Introduction to the Python custom exception To create a custom exception class, you define a class that inherits from the built-in Exception class or one of its subclasses such as ValueError class: The following example defines a CustomException class that inherits from the Exception class: class CustomException(Exception): """ m.. 2023. 4. 7.
Raise From https://www.pythontutorial.net/python-oop/python-raise-from/ Introduction to the Python raise from statement The raise from statement has the following syntax: raise from Technically, it’s equivalent to the following: ex = ExceptionType ex.__cause__ = cause raise ex By default, the __cause__ attribute on exception objects is always initialized to None. Python raise from statement example The fol.. 2023. 4. 7.
Raise Exceptions https://www.pythontutorial.net/python-oop/python-raise-exception/ Introduction to the Python raise statement To raise an exception, you use the raise statement: raise ExceptionType() The ExceptionType() must be subclass of the BaseException class. Typically, it is a subclass of the Exception class. Note that the ExceptionType doesn’t need to be directly inherited from the Exception class. It can.. 2023. 4. 7.
Exception Handling https://www.pythontutorial.net/python-oop/python-exception-handling/ Introduction to the exception handling in Python To handle exceptions, you use the try statement. The try statement has the following clauses: try: # code that you want to protect from exceptions except as ex: # code that handle the exception finally: # code that always execute whether the exception occurred or not else: # code.. 2023. 4. 7.
Exceptions https://www.pythontutorial.net/python-oop/python-exceptions/ Introduction to Python exceptions In Python, exceptions are objects of the exception classes. All exception classes are the subclasses of the BaseException class. However, almost all built-in exception classes inherit from the Exception class, which is the subclass of the BaseException class: This page shows a complete class hierarchy .. 2023. 4. 7.
enum 기타 기능 enum member를 str화 작업 방법 1. enum base class에 __repr__, __str__메서드를 반환값이 self.name으로 overriding한다. from enum import Enum, auto class StrEnum(str, Enum): def _generate_next_value_(name, start, count, last_values): return name def __repr__(self): return self.name def __str__(self): return self.name 2. 1에서 생성한 열거형 클래스를 멤버를 추가하여 상속 생성한다. class Language(StrEnum): HTML = auto() CSS = auto() JS = auto() .. 2023. 4. 7.
Data vs. Non-data Descriptors 출처 : https://www.pythontutorial.net/python-oop/python-data-descriptors/ Python Data vs. Non-data Descriptors Descriptors have two types: Data descriptors are objects of a class that implements __set__ method (and/or __delete__ method) Non-data descriptors are objects of a class that have the __get__ method only. Both descriptor types can optionally implement the __set_name__ method. The __set_na.. 2023. 4. 4.
descriptors 출처 :https://www.pythontutorial.net/python-oop/python-descriptors/ Python Descriptors In this tutorial, you'll learn about Python descriptors, how they work, and how to apply them effectively. www.pythontutorial.net Introduction to the Python descriptors 두 개의 인스턴스 속성 ( first_name와 last_name ) 을 가진 Person 클래스가 있다고 가정합니다. class Person: def __init__(self, first_name, last_name): self.first_name = fi.. 2023. 4. 4.
Multiple inheritance 출처 : https://www.pythontutorial.net/python-oop/python-multiple-inheritance/ Python Multiple Inheritance In this tutorial, you'll learn about Python multiple inheritance and how method order resolution works in Python. www.pythontutorial.net Introduction to the Python Multiple inheritance. 클래스가 단일 클래스에서 상속되면 단일 상속이다. Python을 사용하면 클래스가 여러 클래스에서 상속받을 수 있습니다. 클래스가 둘 이상의 클래스에서 상속되는 경우 multiple inheri.. 2023. 4. 4.