2021년 30주차 기록
2021.07.26
iterable_unpacking argument_unpacking
07/26 (월) ~ 08/01 (일)
python에 unpacking
의 활용
- #iterable_unpacking, #argument_unpacking
- 파이썬에서 쓰는 용어는 각각..
-
iterable unpacking
, (single) asterisk -
dictionary unpacking
, double asterisk
-
- JavaScript 에선
Destructuring assignment
라고 한다
참조 링크
- Unpacking Argument Lists, docs.python.org
- double asterisk (**), dictionary unpacking, docs.python.org
- asterisk (*), iterable unpacking, docs,python.org
- 파이썬의 Asterisk 이해하기, mingrammer.com
- 가변길이 인수목록 받기, Deep Play
- 5 Best Practices of Destructuring Assignments in Python, medium.com
다 단계 상속시 init 의 인자 단순화
- 아버지 클래스에서 단순히 전달만 하는 파라메터를 생략하고 싶을때
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
class BasePage(object): """ 할아버지 클래스 """ def __init__(self, parm1, param2=None, param3=None) pass class BaseLibPage(BasePage): """ 아버지 클래스 """ def __init__(self, *args): # 여기서는 collecting super().__init__(*args) # 여기서는 unpacking class LibDetailPage(BaseLibPage): """ 자식 클래스 """ def __init__(self, param1, param2, param3) super().__init__(param1, param2, param3)
answer
I referenced this book and I found that my code are breaking many rules of inheritance. All the block-quoted part down below came from this book.
- The rules of inheritance I broke
- Substitutability :
Using a different instance creation signature is a common way to violate
substitutability
.- In my code,
BooksBorrowed.Counter
is breaking thesubstitutabiltiy
- In my code,
- Superclass access :
Overriding methods to take different numbers of arguments, and passing only some of them along using super(), can lead to confusion and poor maintainability.
- In my code,
BooksBorrowed.Counter.__init__
is taking diferrent numbers of arguments.
- In my code,
-
has-a
relationshipUse composition for has-a relationships
- In my code, ‘Counter’
has a
‘status-type’. But,status-type
is overrided in the inheritance way.
- In my code, ‘Counter’
- Substitutability :
- How to use the composition way.
Inversion of control says that instaed of creating instances of dependencies in your class, you can pass in existing instances for the clas to make use of.
- There are two relations in my code. They are ‘BaseBookInfo’- ‘Counter’ and ‘Counter’-‘status_type’ They are tightly coupled each outher so it is hard to upgarde. They need to be coupled loosely.