如何访问同一个 class 中另一个函数中定义的函数中的字典
How to access a dictionary in a function that is defined in another function in the same class
我创建了一个 class,里面有两个函数。这些函数 运行 永远在代码底部的循环中。但是,第一个函数创建了一个字典,用户向该字典添加了值。第二个函数是为了导入字典并为每个值加 10。但是,当我 运行 这段代码时,我收到一条错误消息,指出 'Materials is not defined'。我应该如何在这两个函数中正确使用字典?
这是我的代码:
class materialsClass:
def materialsChange(self):
while True:
q1 = raw_input("Type 'edit' to add or change a material, or 'continue' to continue: ")
if q1 == 'edit':
while True:
q2 = raw_input("Type 'add' to add a new material, 'edit' to edit amount of a material: ")
if q2 == 'add':
x = str(raw_input("Enter the Material: "))
y = int(0)
Materials = {x:y}
break
elif q2 == 'edit':
x = str(raw_input("Enter your Material: "))
y = int(raw_input("Enter your Change: "))
Materials[x] += y
break
else:
print "Please Type an Option"
elif q1 == 'continue': break
else:
print "Type an Option!"
print Materials
def materialCounter(self):
for k in Materials: Materials[k] += 10
print Materials
while True:
obj=materialsClass()
obj.materialsChange()
obj.materialCounter()
您不能在另一个方法中使用一个方法的局部变量。您需要在 class 范围内定义变量并使其成为实例变量。例如:
class materialsClass:
def __init__(self):
self.Materials = dict()
def materialsChange(self):
...
self.Materials[x] = y
(in place of Materials = {x:y})
def materialCounter(self):
for k in self.Materials:
self.Materials[k] += 10
print self.Materials
另请注意,当解释器运行该行时
Materials = {x:y}
它将 Materials 字典替换为新的字典,而您实际上并没有在字典中添加新的 material。这就是为什么你应该写:
self.Materials[x] = y
相反。这将在字典中添加一个新的 material。
函数内的变量位于本地命名空间中,因此您不能在第二个函数内使用 Materials
,因为它已在第一个函数中定义!您可以在封闭的 class 范围内初始 Materials
代替:
但请注意,您需要在 __init__
函数中初始化您的变量:
class materialsClass:
def __init__(self):
self.Materials=dict()
def materialsChange(self):
while True:
q1 = raw_input("Type 'edit' to add or change a material, or 'continue' to continue: ")
if q1 == 'edit':
while True:
q2 = raw_input("Type 'add' to add a new material, 'edit' to edit amount of a material: ")
if q2 == 'add':
x = str(raw_input("Enter the Material: "))
y = int(0)
self.Materials = {x:y}
break
elif q2 == 'edit':
x = str(raw_input("Enter your Material: "))
y = int(raw_input("Enter your Change: "))
self.Materials[x] += y
break
else:
print "Please Type an Option"
elif q1 == 'continue': break
else:
print "Type an Option!"
print self.Materials
def materialCounter(self):
for k in self.Materials: self.Materials[k] += 10
print self.Materials
以其他一些答案为基础。您必须在 class 下创建字典,而您向字典中添加项目的方式不正确,所以我已经更改了它。您还必须以正确的方式创建 class 才能使其正常工作。我已经检查了这段代码,无论如何它对我有用。希望对您有所帮助。
class materialsClass(object):
def __init__(self): # create a new class
self.materials = {} # create a new dictionary under this class
def materialsChange(self):
while True:
q1 = raw_input("Type 'edit' to add or change a material, or 'continue' to continue: ")
if q1 == 'edit':
while True:
q2 = raw_input("Type 'add' to add a new material, 'edit' to edit amount of a material: ")
if q2 == 'add':
x = str(raw_input("Enter the Material: "))
y = int(0)
self.materials[x] = y # this is how to add items to dictionaries sequentially
print self.materials
break
elif q2 == 'edit':
x = str(raw_input("Enter your Material: "))
y = int(raw_input("Enter your Change: "))
self.materials[x] = y
print self.materials
break
else:
print "Please Type an Option"
elif q1 == 'continue': break
else:
print "Type an Option!"
print self.materials
def materialCounter(self):
for k in self.materials:
self.materials[k] = self.materials[k] + 10
print self.materials
obj=materialsClass() # you do not need to create the class again and again, just once is fine
while True:
obj.materialsChange()
obj.materialCounter()
我创建了一个 class,里面有两个函数。这些函数 运行 永远在代码底部的循环中。但是,第一个函数创建了一个字典,用户向该字典添加了值。第二个函数是为了导入字典并为每个值加 10。但是,当我 运行 这段代码时,我收到一条错误消息,指出 'Materials is not defined'。我应该如何在这两个函数中正确使用字典?
这是我的代码:
class materialsClass:
def materialsChange(self):
while True:
q1 = raw_input("Type 'edit' to add or change a material, or 'continue' to continue: ")
if q1 == 'edit':
while True:
q2 = raw_input("Type 'add' to add a new material, 'edit' to edit amount of a material: ")
if q2 == 'add':
x = str(raw_input("Enter the Material: "))
y = int(0)
Materials = {x:y}
break
elif q2 == 'edit':
x = str(raw_input("Enter your Material: "))
y = int(raw_input("Enter your Change: "))
Materials[x] += y
break
else:
print "Please Type an Option"
elif q1 == 'continue': break
else:
print "Type an Option!"
print Materials
def materialCounter(self):
for k in Materials: Materials[k] += 10
print Materials
while True:
obj=materialsClass()
obj.materialsChange()
obj.materialCounter()
您不能在另一个方法中使用一个方法的局部变量。您需要在 class 范围内定义变量并使其成为实例变量。例如:
class materialsClass:
def __init__(self):
self.Materials = dict()
def materialsChange(self):
...
self.Materials[x] = y
(in place of Materials = {x:y})
def materialCounter(self):
for k in self.Materials:
self.Materials[k] += 10
print self.Materials
另请注意,当解释器运行该行时
Materials = {x:y}
它将 Materials 字典替换为新的字典,而您实际上并没有在字典中添加新的 material。这就是为什么你应该写:
self.Materials[x] = y
相反。这将在字典中添加一个新的 material。
函数内的变量位于本地命名空间中,因此您不能在第二个函数内使用 Materials
,因为它已在第一个函数中定义!您可以在封闭的 class 范围内初始 Materials
代替:
但请注意,您需要在 __init__
函数中初始化您的变量:
class materialsClass:
def __init__(self):
self.Materials=dict()
def materialsChange(self):
while True:
q1 = raw_input("Type 'edit' to add or change a material, or 'continue' to continue: ")
if q1 == 'edit':
while True:
q2 = raw_input("Type 'add' to add a new material, 'edit' to edit amount of a material: ")
if q2 == 'add':
x = str(raw_input("Enter the Material: "))
y = int(0)
self.Materials = {x:y}
break
elif q2 == 'edit':
x = str(raw_input("Enter your Material: "))
y = int(raw_input("Enter your Change: "))
self.Materials[x] += y
break
else:
print "Please Type an Option"
elif q1 == 'continue': break
else:
print "Type an Option!"
print self.Materials
def materialCounter(self):
for k in self.Materials: self.Materials[k] += 10
print self.Materials
以其他一些答案为基础。您必须在 class 下创建字典,而您向字典中添加项目的方式不正确,所以我已经更改了它。您还必须以正确的方式创建 class 才能使其正常工作。我已经检查了这段代码,无论如何它对我有用。希望对您有所帮助。
class materialsClass(object):
def __init__(self): # create a new class
self.materials = {} # create a new dictionary under this class
def materialsChange(self):
while True:
q1 = raw_input("Type 'edit' to add or change a material, or 'continue' to continue: ")
if q1 == 'edit':
while True:
q2 = raw_input("Type 'add' to add a new material, 'edit' to edit amount of a material: ")
if q2 == 'add':
x = str(raw_input("Enter the Material: "))
y = int(0)
self.materials[x] = y # this is how to add items to dictionaries sequentially
print self.materials
break
elif q2 == 'edit':
x = str(raw_input("Enter your Material: "))
y = int(raw_input("Enter your Change: "))
self.materials[x] = y
print self.materials
break
else:
print "Please Type an Option"
elif q1 == 'continue': break
else:
print "Type an Option!"
print self.materials
def materialCounter(self):
for k in self.materials:
self.materials[k] = self.materials[k] + 10
print self.materials
obj=materialsClass() # you do not need to create the class again and again, just once is fine
while True:
obj.materialsChange()
obj.materialCounter()