简体   繁体   English

Python:当变量被类方法调用时,有一个带有对象变量的“NameError”

[英]Python: there is a 'NameError' with a object variable when the variable is called by a class method

I am making a program with a grid in it, and I need a 2d array.我正在制作一个带有网格的程序,我需要一个二维数组。 In the constructor for the Grid object, I initialize 2 variables, a tuple that holds coordinates, and a 2d array of objects.Grid对象的构造函数中,我初始化了 2 个变量、一个保存坐标的tuple和一个 2d 对象数组。 The tuple(Named, selected) works perfectly. tuple(Named, selected)完美地工作。 The array(named gridArray ) does not work as intended.数组(名为gridArray )无法按预期工作。

When I run the program when the method selectCell is called I get the error "NameError: name 'gridArray' is not defined"当我在调用selectCell方法时运行程序时出现错误"NameError: name 'gridArray' is not defined"

To test it I have turned gridArray into a simple int, and the program gives the same error.为了测试它,我将gridArray变成了一个简单的 int,并且程序给出了同样的错误。 I have also called it with the following:我还用以下方式调用了它:

Grid.gridArray This gives the error that Grid array doesn't have a variable named gridArray Grid.gridArray这给出了 Grid 数组没有名为gridArray的变量的gridArray

self.gridArray The error essentially says self isn't defined. self.gridArray该错误本质上是说self未定义。

Code:代码:

class Grid:
    def _init_(self):
        gridArray = 5      #[[cell.Cell() for j in range(9)] for i in range(9)]
        selected = (-1,-1)

    def selectCell(x):
        selected = (int(x[0]/const.CELLSIZE),int(x[1]/const.CELLSIZE))
        print(selected)
        print(gridArray)

print(gridArray) should just print 5 instead, it is just a NameError print(gridArray)应该只打印 5,它只是一个NameError

You need to refer to the gridArray attribute of a specific instance .您需要参考特定实例gridArray属性。 This is often done with self , and is necessary to distinguish between class variables, instance variables and local variables:这通常用self完成,并且有必要区分类变量、实例变量和局部变量:

class Grid:

    # Class variables are defined here. All instances of the class share references to them.

    def __init__(self):
        # Anything prefixed with "self" is an instance variable. There will be one for each instance of Grid.
        # Anything without is a local variable. There will be one for each time the function is called.
        self.gridArray = 5
        self.selected = (-1, -1)

    def selectCell(self, x):
        self.selected = (int(x[0] / const.CELLSIZE),int(x[1] / const.CELLSIZE))
        print(self.selected)
        print(self.gridArray)

Every function in a class needs to parse the self variable for it to reference other variables defined in the instance of the class when it's created.类中的每个函数都需要解析self变量,以便在创建时引用类实例中定义的其他变量。 Right now, gridArray is a local variable in your __init__ function.现在, gridArray 是__init__函数中的一个局部变量。 You can read more about classes herehttps://docs.python.org/3.7/tutorial/classes.html#class-objects您可以在此处阅读有关类的更多信息https://docs.python.org/3.7/tutorial/classes.html#class-objects

You should define gridArray as self.gridArray so that you can use it other places in your class.您应该将gridArray定义为self.gridArray以便您可以在类中的其他地方使用它。 Be sure to also parse the self variable in every function that belongs to a class like this: def selectCell(self, x): the general format is def <funcname>(self, *args): <code> .一定还要解析属于这样的类的每个函数中的self变量: def selectCell(self, x):一般格式是def <funcname>(self, *args): <code>

Also, the __init__ function should have 2 underscores before and after.此外, __init__函数前后应该有 2 个下划线。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM