Python初学者Class变量错误

Python beginner Class variable Error

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

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

如果我将变量放在 class 之外,它就可以工作。 我做错了什么??

感谢您的帮助。

你需要使用b.width和b.height来引用成员变量定义。 有关访问成员变量的一些解释,请参阅此 post - Accessing a class' member variables in Python?

来自docs

Names refer to objects. Names are introduced by name binding operations. Each occurrence of a name in the program text refers to the binding of that name established in the innermost function block containing the use.

A block is a piece of Python program text that is executed as a unit. The following are blocks: a module, a function body, and a class definition. Each command typed interactively is a block. A script file (a file given as standard input to the interpreter or specified as a command line argument to the interpreter) is a code block. A script command (a command specified on the interpreter command line with the ‘-c‘ option) is a code block. The string argument passed to the built-in functions eval() and exec() is a code block.

A code block is executed in an execution frame. A frame contains some administrative information (used for debugging) and determines where and how execution continues after the code block’s execution has completed.

A scope defines the visibility of a name within a block. If a local variable is defined in a block, its scope includes that block. If the definition occurs in a function block, the scope extends to any blocks contained within the defining one, unless a contained block introduces a different binding for the name. The scope of names defined in a class block is limited to the class block; it does not extend to the code blocks of methods – this includes comprehensions and generator expressions since they are implemented using a function scope. This means that the following will fail:

class A:
    a = 42
    b = list(a + i for i in range(10))

python3 中的列表组件有自己的范围,而不是 python2 您的代码可以按原样工作的范围。

如果您使用 python2 进行以下示例,您可以看到泄漏列表 comp 范围的变量如何导致一些问题:

class A:
    a = 42
    b = [a for a in range(10)]

a = A()

print(a.a)
9

您有几个选项,您可以使用 __init__ 创建实例属性:

class Map:
    def __init__(self):
        self.height = 11
        self.width = 21
        self.top = [['#']*self.width]
        self.middle = [['#']+[' ']*(self.width-2)+['#'] for i in range(self.height-2)]
        self.field = self.top + self.middle + self.top
m=Map()
print(m.field)

使用方法:

class Map:
    @staticmethod
    def meth():
        height = 11
        width = 21
        top = [['#']*width]
        middle = [['#']+[' ']*(width-2)+['#'] for i in range(height-2)]
        field = top + middle + top
        return field

b = Map()


print(b.meth())

你选择什么取决于你想做什么。

我也是Python的初学者

使用init()函数定义变量

class Map:
    def __init__(self,height,width,top,middle,field):
        self.height = height
        self.width = width
        self.top = top
        self.middle = middle
        self.field = field

另外,在 YouTube 上观看一些 OOP Python 初学者教程。他们真的很有帮助