在__init__外部定义的实例属性ship_speed_factor。为什么?

时间:2019-10-26 14:09:31

标签: python-3.x

我一次又一次遇到同样的问题,不明白为什么。我正在使用Pycharm进行编码,也许是我不确定的设置。

问题: 实例属性ship_speed_factor在 init 之外定义。

如果我要在运行时用户更改变量时调用此变量,为什么ship_speed_factor不能在 init 之外。

def initialize_dynamic_settings(self):
        self.ship_speed_factor = 1.5
        self.bullet_speed_factor = 3
        self.alien_speed_factor = 1

        self.fleet_direction = 1

这是完整的代码

enter code here
class Settings():
"""A class to store all settings for Alien Invasion"""
def __init__(self):
    """"Initialize the game's static settings."""
    # Screen settings
    self.screen_width = 1100
    self.screen_height = 800
    self.bg_color = (230, 230, 230)

    # ship settings
    self.ship_limit = 3

    # bullet settings
    self.bullet_width = 6
    self.bullet_height = 15
    self.bullet_color = (60, 60, 60)
    self.bullets_allowed = 4

    # Alien settings
    self.fleet_drop_speed = 10

    # How quickly the game speed up
    self.speedup_scale = 1.1

    self.initialize_dynamic_settings()

def increase_speed(self):
    self.ship_speed_factor *= self.speedup_scale
    self.bullet_speed_factor *= self.speedup_scale
    self.alien_speed_factor *= self.speedup_scale

def initialize_dynamic_settings(self):
    self.ship_speed_factor = 1.5
    self.bullet_speed_factor = 3
    self.alien_speed_factor = 1

    self.fleet_direction = 1

1 个答案:

答案 0 :(得分:0)

这可能是PyCharm发出的警告,不是错误。 这样做的原因是linter会检查实例属性是否在__init__方法中定义,因为人们可能希望在那里找到它。

请参见Instance attribute attribute_name defined outside __init__