How do I correctly add type-hints to Mixin classes?

后端 未结 5 1813
梦谈多话
梦谈多话 2021-02-13 02:33

Consider the following example. The example is contrived but illustrates the point in a runnable example:

class MultiplicatorMixin:

    def multiply(self, m: in         


        
5条回答
  •  执念已碎
    2021-02-13 02:56

    I've tested it on my machine, hope it will also work for you:

    class MultiplicatorMixin:
        value = None # type: int
    
        def multiply(self, m: int) -> int:
            return self.value * m
    
    
    class AdditionMixin:
        value = None # type: int
    
        def add(self, b: int) -> int:
            return self.value + b
    
    
    class MyClass(MultiplicatorMixin, AdditionMixin):
    
        def __init__(self, value: int) -> None:
            self.value = value
    
    
    instance = MyClass(10)
    print(instance.add(2))
    print(instance.multiply(2))
    

提交回复
热议问题