Python 如何将变量指定给对象名?
尝试了以下操作,其中“objectname”包含一个字符串名,将在创建对象时分配Python 如何将变量指定给对象名?,python,Python,尝试了以下操作,其中“objectname”包含一个字符串名,将在创建对象时分配 for record in result: objectname = 'Customer' + str(record[0]) print objectname customername = str(record[1]) objectname = Customer(customername) 其中客户是一个类 在我的测试中,这个循环运行了两次,将“objectname”打印为Cust
for record in result:
objectname = 'Customer' + str(record[0])
print objectname
customername = str(record[1])
objectname = Customer(customername)
其中客户是一个类
在我的测试中,这个循环运行了两次,将“objectname”打印为Customer1和Customer2,但创建了两个对象,但是这些对象被称为“objectname”(它覆盖每个循环),而不是两个唯一的对象Customer1或Customer2
它只是不在变量内部分配字符串(customer1,2),而只是分配变量名
我曾尝试将字符串指定给对象名,但这会导致语法错误
当然,必须一直这样做,提前感谢您的帮助。您可以将对象存储在Python字典中,而不是为每个客户使用新变量:
d = dict()
for record in result:
objectname = 'Customer' + str(record[0])
customername = str(record[1])
d[objectname] = Customer(customername)
print d
customers = {}
for record in result:
objectname = 'Customer' + str(record[0])
customers[customername] = Customer(str(record[1])) #assignment to dictionary
字典中存储的对象示例
我就是忍不住自己写了一些代码(比我开始要做的还多)。就像上瘾一样。无论如何,我不会用对象来做这类工作。我可能会使用sqlite数据库(如果需要,可以保存在内存中)。但这段代码向您展示了(希望如此)如何使用字典将包含客户数据的对象保存在:
# Initiate customer dictionary
customers = dict()
class Customer:
def __init__(self, fname, lname):
self.fname = fname
self.lname = lname
self.address = None
self.zip = None
self.state = None
self.city = None
self.phone = None
def add_address(self, address, zp, state, city):
self.address = address
self.zip = zp
self.state = state
self.city = city
def add_phone(self, number):
self.phone = number
# Observe that these functions are not belonging to the class.
def _print_layout(object):
print object.fname, object.lname
print '==========================='
print 'ADDRESS:'
print object.address
print object.zip
print object.state
print object.city
print '\nPHONE:'
print object.phone
print '\n'
def print_customer(customer_name):
_print_layout(customers[customer_name])
def print_customers():
for customer_name in customers.iterkeys():
_print_layout(customers[customer_name])
if __name__ == '__main__':
# Add some customers to dictionary:
customers['Steve'] = Customer('Steve', 'Jobs')
customers['Niclas'] = Customer('Niclas', 'Nilsson')
# Add some more data
customers['Niclas'].add_address('Some road', '12312', 'WeDon\'tHaveStates', 'Hultsfred')
customers['Steve'].add_phone('123-543 234')
# Search one customer and print him
print 'Here are one customer searched:'
print 'ooooooooooooooooooooooooooooooo'
print_customer('Niclas')
# Print all the customers nicely
print '\n\nHere are all customers'
print 'oooooooooooooooooooooo'
print_customers()
你需要的是一本字典:
d = dict()
for record in result:
objectname = 'Customer' + str(record[0])
customername = str(record[1])
d[objectname] = Customer(customername)
print d
customers = {}
for record in result:
objectname = 'Customer' + str(record[0])
customers[customername] = Customer(str(record[1])) #assignment to dictionary
通常,动态生成变量名并没有多大用处。我当然会建议像Niclas的答案,但如果你知道这是你想要的,这里是你可以做到的:
for record in result:
objectname = 'Customer' + str(record[0])
print objectname
customername = str(record[1])
exec '%s = Customer(%r)' % (customername, customername)
这将导致变量Customer1
和Customer2
被添加到最内部的作用域中,与执行以下行完全相同:
Customer1 = Customer('Customer1')
Customer2 = Customer('Customer2')
这样做时,您需要确保
customername
是一个。它一直在尝试,但不需要。事实上,这是一种非常糟糕的方式。只需使用集合。请更正示例代码中的缩进。你知道,这在Python中很重要。感谢大家的回复,感谢大家对我的Python新手知识的关心。好吧,Rotem,使用与我相同的解决方案的字典有一个更好的名字——“customers”。否则它们几乎是一样的:)对不起,当你在写你的东西的时候开始写了,答案显然是一样的。谢谢你的回复和你的时间,我可以看到你正在创建一个字典,但看起来像是你在给字典分配对象,反对将值放入字典,然后将其分配给对象。这显然是正确的方法,但对我来说似乎有违直觉很好,假设您有一个名为Steve的客户,如果您想为他检索customer对象(可能包含Address、phone numner等),那么您可以编写d['Steve'],然后返回该对象。这对我来说似乎很直观:)我现在明白你的意思了,我才刚开始使用对象,所以我一直在用Steve=Customer()创建一个类并为一个对象分配一个名称,然后通过键入Steve返回。感谢你的回复和你的时间,我可以看到你正在创建一个字典,但它看起来像是将对象分配给字典,而不是将值放入字典,然后将其分配给对象。这显然是正确的方法,但对我来说似乎有违直觉我无法想象它以何种方式看起来更复杂或违反直觉,但我想对每个人来说都是自己的……谢谢你的帮助!这有助于我更好地理解总体思路!这是唯一回答原始问题的答案。字典里的答案并没有错,也并没有解决原来的问题。