python에 unpacking의 활용

  • #iterable_unpacking, #argument_unpacking
  • 파이썬에서 쓰는 용어는 각각..
    • iterable unpacking, (single) asterisk
    • dictionary unpacking, double asterisk
  • JavaScript 에선 Destructuring assignment라고 한다

참조 링크

다 단계 상속시 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 the substitutabiltiy
    • 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.
    • has-a relationship

      Use composition for has-a relationships

      • In my code, ‘Counter’ has a ‘status-type’. But, status-type is overrided in the inheritance way.
  • 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.