Instance attribute attribute_name defined outside __init__

后端 未结 6 1066
不知归路
不知归路 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:26

    Although the definition of instance variables outside init isn't recommended in general, there are rare cases in which it is natural. For example, when you have a parent class that defines several variables that its child classes won't use, and whose definition will make its child waste time or resources, or will be simply unaesthetic.

    One possible solution to this is using an init-extention function, that each child class may override, and in this function use function setattr in order to define the class-unique instance variables. May be this is not too aesthetic as well, but it eliminates the here-discussed linting warning.

提交回复
热议问题