【发布时间】:2013-09-04 17:39:58
【问题描述】:
我有这样的课程:
class Rank (models.Model):
respect=models.IntegerField(max_length=3)
#some other attributes
Rank 有很多属性(第一个是respect)。
Rank的属性名称存储在list、attributes中。
我想遍历它们,将它们设置为somevalue,一个任意变量。
我正在尝试:
rank=Rank()
for att in attributes:
rank.att=somevalue #I want rank.(value of att) instead of this
【问题讨论】:
-
你试图将变量的值而不是变量名发送到哪个函数?
-
类的构造函数看代码就知道了
-
你的问题有点误导。 Python 使用按引用传递,按值传递的唯一方法是使用对象的深层副本(这非常低效)。此外,除了 init 之外,我什至没有在您的代码中看到任何函数。
-
@ShashankGupta 不,python 不 使用通过引用传递。如果是这样,这个问题就会有一定的意义。
-
我认为他希望
rank.att实际上是指rank.{value_of_att}。
标签: python django function class attributes