当前位置: 动力学知识库 > 问答 > 编程问答 >

python - When assigning my objects to a grid, the attributes aren't there

问题描述:

The code I currently have which does not work

import pprint

class Kile(object):

def __init__(self, name):

self.name = name

kilevalue = 1

def checkkilevalue(self):

print (self.kilevalue)

kilecounter = 0

krid = []

for row in range(4):

krid.append([])

for colomn in range(4):

krid[row].append([])

for level in range(3):

krid[row][colomn].append([])

for tile in range(1):

krid[row][colomn][0].append(Kile(kilecounter))

kilecounter += 1

print(kilecounter)

print(krid[0][0][0])

pprint.pprint(krid)

print(krid[0][0][0].kilevalue)

It shows the grid, it shows objects on the [row][colomn][0] places but it does not have the .kilevalue attribute

And now the other way it does work, but the way I don't need it.

class Tile(object):

def __init__(self, name):

self.name = name

tilevalue = 0

def checktilevalue(self):

print (self.tilevalue)

trid = []

ttilecounter = 0

for row in range(3):

trid.append([])

for colomn in range(3):

trid[row].append(Tile(ttilecounter))

ttilecounter += 1

print(ttilecounter)

pprint.pprint(trid)

print(dir(trid[0][0]))

网友答案:

Your kile object does not have a kilevalue instance variable. In your __init__() method, you are just creating a local variable with name kilevalue not an instance variable.

You need to assign 1 to self.kilevalue in the __init__() method for kilevalue to be an instance variable -

def __init__(self, name):
        self.name = name
        self.kilevalue = 1
网友答案:

In the __init__ method of Kile, you are assigning kilevalue = 1 but you are using it as a property.

I think you need to change this to self.kilevalue = 1 to be able to use it the way you want to.

A note: you are also defining the checkkilevalue inside __init__ method. It will not be accessible from anywhere else (assuming that the indentation is correct in your example)

网友答案:

I think your error is that you assign kilevalue only in the function itself. I think you want that the kilevalue is assigned to the class.

To do so, Change kilevalue = 1 to self.kilevalue = 1

EDIT: I think krid[row][colomn][0] is always None, because you're not Setting something to it. Try adding krid[row][colomn][0] = [] before krid[row][colomn][0].append(Kile(kilecounter))

网友答案:

so after a bit of tinkering and trying the answers and some more tinkering i found the solution. Although i dont truly understand it.

class Kile(object):
    def __init__(self, name):
        self.name = name

    kilevalue = 2

    def checkkilevalue(self):
        print (self.kilevalue)

krid = []
kilecounter = 0
for row in range(4):
    krid.append([])
    for colomn in range(4):
        krid[row].append([])
        for level in range(1):
            krid[row][colomn].append(Kile(kilecounter))
            kilecounter += 1
        for level in range(2):
            krid[row][colomn].append([])

so first i append the object to a 3rd list and then afterwards append two other empty list items to that list. and that works. Anyway thanks for the support :)

分享给朋友:
您可能感兴趣的文章:
随机阅读: