小编Coo*_*elp的帖子

Python初学者类变量Error

这是我的第一个问题,很抱歉......我是python和编码的初学者,我想创建一个名为'Map'的类,它将具有以下类变量:

class Map:
    height = 11  
    width = 21

    top = [['#']*width]
    middle = [['#']+[' ']*(width-2)+['#'] for i in range(height-2)]
    field = top + middle + top

b = Map()

Shell:
>>> middle = [['#']+[' ']*(width-2)+['#'] for i in range(height-2)]
    NameError: name 'width' is not defined
Run Code Online (Sandbox Code Playgroud)

如果我将变量放在类之外就可以了.我究竟做错了什么??

谢谢您的帮助.

python class class-variables python-3.x

8
推荐指数
1
解决办法
240
查看次数

标签 统计

class ×1

class-variables ×1

python ×1

python-3.x ×1