我试图用变量名创建对象,当我打印出objectname变量时,会为其指定正确的名称。但是当我尝试使用objectname变量创建一个对象时,创建的对象实际上被称为“objectname”,而不是使用分配给变量的字符串。我的代码如下:class Customer:
# Initiliaise method, creating a customer object
def __init__(self,name):
self.name = name
print "Customer %s Added" % (self.name)
# Print out details
def output(self):
print "This is the customer object called %s" % (self.name)
## Create the Customer objects, from the Customer table
# Pull the Customers out of the Customer table
# SQL
cursor.execute("SELECT * FROM Customer")
result = cursor.fetchall()
for record in result:
objectname = 'Customer' + str(record[0])
print objectname # This prints "Customer1..2" etc
# customername is the exact name as in the database
customername = str(record[1])
# Use the above variables pulled from the database to create a customer object
objectname=Customer(customername)
# We need to count the number of customer objects we create
customercount = customercount + 1
因此,只需创建一个名为objectname的对象,而不是基于Customer DB表中的数字的多个对象“Customer1,2,3”等。变量名基于字符串“Customer”和数据库中的行ID。在
我假设我引用的变量不正确
谢谢你的帮助。在