python
-
Liskov Substitution Principlepython/SOLID 2023. 12. 6. 22:53
정의 Subclasses should NOT change the behavior of superclasses in unexpected ways. Selecting on types 유형 위반 코드 class Employee: def __init__(self, name): self.name = name class Manager(Employee): def __init__(self, name, department): super().__init__(name) self.department = department def print_employee(e): if type(e) is Employee: print(f"{e.name} is an employee") elif type(e) is Manager: print(f"{..
-
Open-closed Principlepython/SOLID 2023. 12. 6. 20:27
정의 Open for extension but closed for modification. 위반 코드 class Employee: def __init__(self, name): self.name = name class Manager(Employee): def __init__(self, name, department): super().__init__(name) self.department = department def print_employee(e): if type(e) is Employee: print(f"{e.name} is an employee") elif type(e) is Manager: print(f"{e.name} leads department {e.department}") print_empl..
-
Single Responsibility Principlepython/SOLID 2023. 12. 5. 20:11
정의 Things should have only one reason to change. Mixing Resposibility 유형 위반 코드 class Employee: xml_filename = "emp.xml" def __init__(self, name, salary): self.name = name self.salary = salary def raise_salary(self, factor): return self.salary * factor def save_as_xml(self): with open(self.xml_filename, "w") as file: file.write(f"{self.name}") 위의 Employee 클래스는 business logic (raise_salary) 와 stor..
-
yield 활용 2python/응용 2023. 11. 15. 11:22
개선 전 for row in range(height): for col in range(width): value = spreadsheet.get_value(col, row) do_something(value) if this_is_my_value(value): break # ??? 이중 루프를 벗어날 수 없다. Exception 으로 처리 ??? 개선 후 def range_2d(width, heigth): for y in range(height): for x in range(width): yield x, y for col, row in range_2d(widht, height): value = spreadsheet.get_value(col, row) do_something(value) if this_is_m..
-
yield 활용 1python/응용 2023. 11. 15. 11:14
개선 전 f = open("my_config.ini") for line in f: line = line.strip() if line.startswith('#'): # A comment line, skip it continue if not line: # A blank line, skip it continue do_something(line) 개선 후 f = open("my_config.ini") def interesting_lines(f): for line in f: line = line.strip() if line.startswith('#'): continue if not line: continue yield line with open("my_config.ini") as f: for line in int..
-
Decorators with parameterspython/응용 2023. 10. 17. 17:17
https://stackoverflow.com/a/25827070 Transleates decorator without parameters @decorator def foo(*args, **kwargs): pass translates to foo = decorator(foo) Transleates decorator with parameters @decorator_with_args(arg) def foo(*args, **kwargs): pass translates to foo = decorator_with_args(arg)(foo) decorator_with_args(arg) 은 function 을 파라미터로 받는 함수(decorator without parameters)가 되어야 한다. 코드 import..
-
type hints Self typepython/응용 2023. 10. 17. 16:47
https://stackoverflow.com/a/33533514 How do I type hint a method with the type of the enclosing class? I have the following code in Python 3: class Position: def __init__(self, x: int, y: int): self.x = x self.y = y def __add__(self, other: Position) -> Position: stackoverflow.com from __future__ import annotations from dataclasses import dataclass @dataclass class Vector: x: float y: float def ..
-
학습 자료python 2023. 9. 11. 16:37
문서 간단 요약: https://learnxinyminutes.com/docs/python/ cheat sheet: https://github.com/gto76/python-cheatsheet learn python with pytest: https://github.com/trekhleb/learn-python The Hitchhiker’s Guide to Python: https://docs.python-guide.org/ 공식 문서: https://docs.python.org/ko/3/index.html 동영상 YouTube - ArjanCodes: https://www.youtube.com/@ArjanCodes