Python初学者类变量Error [英] Python beginner Class variable Error
问题描述
这是我的第一个问题,很抱歉...
我是一个初学者python和编码,一般来说,我想创建一个名为Map的类,它将有以下类变量: / p>
this is my first question, so sorry... I'm a beginner with python and coding in general, and i wanted to create a class called 'Map' that would have the following class variables:
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
如果我把变量放在类外面,它工作。
我做错了什么?
If i put the variables outside of the class it works. What am I doing wrong??
感谢您的帮助。
推荐答案
从文档:
名称引用对象。名称是通过名称绑定操作引入的。
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.
一个块是一段Python程序文本作为一个单元执行。以下是块:模块,函数体和类定义。交互式输入的每个命令都是一个块。脚本文件(作为解释器的标准输入或作为解释器的命令行参数指定的文件)是代码块。脚本命令(在解释器命令行中使用-c选项指定的命令)是一个代码块。传递给内置函数eval()和exec()的字符串参数是一个代码块。
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))
list comps in python3有你自己的范围,而不是python2,你的代码将工作原样。
list comps in python3 have their own scope, as opposed to python2 where your code would work as is.
如果你使用python2下面的例子,你可以看到变量如何泄露的范围列表comp可能会导致一些问题:
If you take the following example using python2, you can see how variables leaking the scope of the list comp could cause some problems:
class A:
a = 42
b = [a for a in range(10)]
a = A()
print(a.a)
9
您有几个选项,您可以使用 __ init __
创建实例属性:
You have a few options, you could use __init__
creating instance attributes:
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())
你选择的是什么取决于你想做什么。
What you choose is really dependant on what you want to do.
这篇关于Python初学者类变量Error的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!