【问题标题】:Why does an indentation error cause shadowing from outer scopes为什么缩进错误会导致外部范围的阴影
【发布时间】:2020-08-15 16:36:47
【问题描述】:

我正在开发一款名为 Scuba Adventure 的游戏,在尝试查找属性错误的来源时,我遇到了另一个错误,上面写着“来自外部范围的物体阴影”。我发现错误的根源是缩进。在定义一个名为 Bubble 的类来控制气泡对象和精灵组时,我不小心将 def __init__() 中的方法缩进了。我的课是这样的:

import pygame
from pygame.sprite import Sprite

class Bubble(Sprite):
    """A class that manages bubbles released from the diver."""

    def __init__(self, sa_game):
        """create a bubble object at the diver's current position."""
        super().__init__()
        """Some attributes"""
        "
        "

        # import the bubble image
        """Code to import the bubble image"""

        # Store the bubble's position as a decimal value
        """ """

        def update(self):
           """Move the bubble up the screen."""
           # Method to update the decimal position of the bubble
           

        def blit_bubble(self):
           """Method to draw the bubble at the diver's current location"""

函数体本身并不重要。但我想知道的是“从外部范围阴影”是什么意思,为什么缩进错误会抛出它。我还是个初学者,所以这个概念对我来说可能是新概念

【问题讨论】:

    标签: python scope


    【解决方案1】:

    这意味着方法存在于两个地方,__init__ 内部的方法将覆盖(阴影)它们在此范围之外的任何定义。

    【讨论】:

    • 这是基础 Python 的一部分吗?我猜它来自 Pygame
    • 啊,它看起来像是 linting,我将“错误”与警告混为一谈。纳米
    • 它来自 Pygame
    • @tripleee,所以我得到的 update() 方法会导致阴影,因为我在其他类中使用了其他 update() 函数。但是 blit_bubble() 是这个类独有的方法,而不是主循环中的函数调用。那么在这种情况下, blit_bubble() 也会被掩盖吗?
    • 如果没有其他定义,那么技术上没有,但是静态代码分析器可能无法排除存在另一个定义的可能性,尽管它看不到它,或者正确但有点误导警告您隐藏未定义的引用。
    猜你喜欢
    • 1970-01-01
    • 2015-10-13
    • 2013-12-06
    • 2016-08-22
    • 2021-10-03
    • 2015-10-23
    • 1970-01-01
    • 2010-12-04
    相关资源
    最近更新 更多