Instance attribute attribute_name defined outside __init__

后端 未结 6 1024
不知归路
不知归路 2020-11-28 04:18

I split up my class constructor by letting it call multiple functions, like this:

class Wizard:
    def __init__(self, argv):
        self.parse_arguments(ar         


        
6条回答
  •  执笔经年
    2020-11-28 04:47

    If you are using Python 3, you can try

    class Wizard:
        def __init__(self, argv):
            self.name: str = str()
            self.magic_ability: str = str()
            self.parse_arguments(argv)
            self.wave_wand() # declaration omitted
    
        def parse_arguments(self, argv):
            if self.has_correct_argument_count(argv):
                self.name = argv[0]
                self.magic_ability = argv[1]
            else:
                raise InvalidArgumentsException() # declaration omitted
    
    # ... irrelevant functions omitted
    

    Although not as pythonic as the accepted answer, but it should get away the Pylint alert.

    And if you don't concern about type and don't want to create a new object with object() use:

    class Wizard:
        def __init__(self, argv):
            self.name = type(None)()
            # ...
    

    As None will cause type not match error.

提交回复
热议问题