当我将列表编号设置为类内的全局变量时,会出现此错误。这是我的代码:
class SomeClass:
numbers = [4, 6, 8, 2, 10]
def incrementNumbers(self):
global numbers
for i in numbers:
i += 1
print (i)
someObject = SomeClass()
someObject.incrementNumbers()
当我运行代码时,我得到错误"名称‘数字’没有定义"。如果我声明"数字"是全局的,为什么不被定义?
要解决作用域问题,请在类之外声明变量(使其成为真正的全局变量(或将其作为类变量进行访问,如下所示。顺便说一句,您的代码不会增加列表项。如果您想增加列表项,请按索引而不是按值引用它们:
class SomeClass:
numbers = [4, 6, 8, 2, 10]
def incrementNumbers(self):
for i in range(len(SomeClass.numbers)):
SomeClass.numbers[i] += 1
print(SomeClass.numbers)
要访问类函数中同一对象的属性,需要使用self
class SomeClass:
numbers = [4, 6, 8, 2, 10]
def incrementNumbers(self):
for i in self.numbers:
i += 1
print(i)
someObject = SomeClass()
someObject.incrementNumbers()
您声明的变量numbers
不是全局变量,而是类变量。
如果你想要一个全局变量,那么你必须写:
numbers = [4, 6, 8, 2, 10]
class SomeClass:
def incrementNumbers(self):
# next line would be only needed if numbers were a
# simple type (like for example an int) and if you wanted
# to modify it or if it were any type and you want to be allowed to assign to it (same as modyfing a simple type) from inside this function.
# So for your exact example you can just leave this line of
# Thanks @juanpa.arrivillaga for pointing this out.
# global numbers
for i in numbers:
i += 1
print (i)
someObject = SomeClass()
someObject.incrementNumbers()
此外,如果您愿意,inc是持久的,那么您必须编写(如@DYZ所述(numbers[i] += 1
而不是i += 1
对于类别varSomeClass.numbers[i] += 1
或this.__class__.numbers[i] += 1
但也许你指的不是全局var,而是一个类var。
那么@DYZ的答案可能很有趣,不过我想用一个稍微复杂一点的例子来详细说明。
它强调了全局变量、类变量、对象的类变量和特定类的类变量之间的区别,如果您有类继承,这可能很重要。
请注意,只要对象没有numbers
属性,那么self.numbers
和this.__class__.numbers
在读取时表现相同,但在分配时表现不同。
#!/usr/bin/env python
numbers = ["g1", "g2"]
class SomeClass:
numbers = [4, 6, 8, 2, 10]
def func_1(self):
print(" numbers is", numbers)
print("self numbers is", self.numbers)
print("class numbers is", SomeClass.numbers)
print("class of self numbers is", self.__class__.numbers)
print()
class SomeOtherClass:
numbers = [4, 6, 8, 2, 10]
def __init__(self):
self.numbers = ["a", "b", "c"]
def func_1(self):
print(" numbers is", numbers)
print("self numbers is", self.numbers)
print("class numbers is", SomeClass.numbers)
print("class of self numbers is", self.__class__.numbers)
print()
class DerivedClass(SomeClass):
numbers = [1, 2]
class OtherDerivedClass(SomeClass):
numbers = [3, 4]
def __init__(self):
self.numbers = ["o1", "o2", "o3"]
c1 = SomeClass()
c2 = SomeOtherClass()
c3 = DerivedClass()
c4 = OtherDerivedClass()
c1.func_1()
c2.func_1()
c3.func_1()
c4.func_1()
上面例子的输出是:
numbers is ['g1', 'g2']
self numbers is [4, 6, 8, 2, 10]
class numbers is [4, 6, 8, 2, 10]
class of self numbers is [4, 6, 8, 2, 10]
numbers is ['g1', 'g2']
self numbers is ['a', 'b', 'c']
class numbers is [4, 6, 8, 2, 10]
class of self numbers is [4, 6, 8, 2, 10]
numbers is ['g1', 'g2']
self numbers is [1, 2]
class numbers is [4, 6, 8, 2, 10]
class of self numbers is [1, 2]
numbers is ['g1', 'g2']
self numbers is ['o1', 'o2', 'o3']
class numbers is [4, 6, 8, 2, 10]
class of self numbers is [3, 4]
如果你需要解释为什么你会得到预期的输出,请毫不犹豫地发表评论,我会尽力澄清