Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/337.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在循环中重新指定对象?_Python_Python 3.x_Python 2.7 - Fatal编程技术网

Python 如何在循环中重新指定对象?

Python 如何在循环中重新指定对象?,python,python-3.x,python-2.7,Python,Python 3.x,Python 2.7,[当前代码]: a = table.a b = table.b c = table.c [预期输出] all = [a, b, c] for i in all: i = table.i 在中修改i以获得所有中的i将不会更改all的元素,但您可以这样迭代: class Table(object): pass table = Table() table.a = 'AAA' table.b = 'BBB' table.c = 'CCC' all = ['a', 'b', 'c'] fo

[当前代码]:

a = table.a
b = table.b
c = table.c
[预期输出]

all = [a, b, c]
for i in all:
 i = table.i

中修改
i
以获得所有
中的i将不会更改
all
的元素,但您可以这样迭代:

class Table(object):
  pass

table = Table()
table.a = 'AAA'
table.b = 'BBB'
table.c = 'CCC'

all = ['a', 'b', 'c']

for idx in range(len(all)):
    all[idx] = getattr(table, all[idx])

print(all)
输出:

['AAA', 'BBB', 'CCC']

中修改
i
以获得所有
中的i将不会更改
all
的元素,但您可以这样迭代:

class Table(object):
  pass

table = Table()
table.a = 'AAA'
table.b = 'BBB'
table.c = 'CCC'

all = ['a', 'b', 'c']

for idx in range(len(all)):
    all[idx] = getattr(table, all[idx])

print(all)
输出:

['AAA', 'BBB', 'CCC']

我认为这更接近您的意图,使用dict作为动态变量的存储库:

class Table(object):
    def __init__(self):
        self.a = None
        self.b = None
        self.c = None
        self.d = None
        self.e = None

table = Table()
table.a = 1
table.b = 2
table.c = 3
table.d = 4
table.e = 5

# copy only this variables
some_vars = ['a', 'b', 'c']

# this line does the trick
my_vars = { var: getattr(table, var) for var in some_vars }
现在,我们已将对象的属性捕获为字典中的键值对:

my_vars['a']
=> 1
my_vars['b']
=> 2
my_vars['c']
=> 3

我认为这更接近您的意图,使用dict作为动态变量的存储库:

class Table(object):
    def __init__(self):
        self.a = None
        self.b = None
        self.c = None
        self.d = None
        self.e = None

table = Table()
table.a = 1
table.b = 2
table.c = 3
table.d = 4
table.e = 5

# copy only this variables
some_vars = ['a', 'b', 'c']

# this line does the trick
my_vars = { var: getattr(table, var) for var in some_vars }
现在,我们已将对象的属性捕获为字典中的键值对:

my_vars['a']
=> 1
my_vars['b']
=> 2
my_vars['c']
=> 3

如果这段代码不在函数内(或者,如果它在函数内但
a
b
c
在该函数以外的任何地方都不需要),则无需使用另一个容器即可通过更新
locals
dict:

attributes = ['a', 'b', 'c']
for attr in attributes:
    locals()[attr] = getattr(table, attr)
例如:

table = type('table', (), {'a': 1})()

attributes = ['a']
for attr in attributes:
    locals()[attr] = getattr(table, attr)

print(a)
输出

1


请注意后果:如果在该范围内存在另一个变量
a
b
c
(不应该存在,因为它们是错误的变量名),它们将被覆盖

如果这段代码不在函数内部(或者,如果它在函数内部但
a
b
c
在该函数之外的任何地方都不需要),则无需使用另一个容器即可通过更新
局部变量
dict:

attributes = ['a', 'b', 'c']
for attr in attributes:
    locals()[attr] = getattr(table, attr)
例如:

table = type('table', (), {'a': 1})()

attributes = ['a']
for attr in attributes:
    locals()[attr] = getattr(table, attr)

print(a)
输出

1


请注意后果:如果在该范围内存在另一个变量
a
b
c
(不应该存在,因为它们是错误的变量名),它们将被覆盖

对。我认为这是可能的。但是更新
all
列表
[“a”、“b”、“c”]
注意
i=table.i
在循环中是无用的代码,您只是重新分配一个迭代变量,而不是创建将位于循环外部的新变量是的。我认为这是可能的。但是更新
all
列表
[“a”、“b”、“c”]
注意
i=table.i
在循环中是无用的代码,您只是重新分配一个迭代变量,而不是创建将位于循环外的新变量。。。但是您已经丢失了变量的名称,它只是一个值列表,无法通过名称引用它们。。。。但是您已经丢失了变量的名称,它只是一个值列表,无法通过名称引用它们。