2011-12-30 103 views
1

尝试了以下操作,其中“objectname”包含字符串名称,要在创建对象时进行分配。如何将变量分配给对象名称?

for record in result: 
    objectname = 'Customer' + str(record[0]) 
    print objectname 
    customername = str(record[1]) 
    objectname = Customer(customername) 

如果客户是一类。

在我的测试,这个循环运行两次打印“对象名”为customer1表和顾客2,但创建2个对象,但对象被称为“对象名”(它覆盖每个循环),相对于2个唯一对象customer1表或顾客2 。

它根本不在变量中分配字符串(customer1,2),而是纯粹的变量名称。

我试图对象名称指定字符串,但给人的语法错误

当然这是一定要做所有的时间,感谢您的帮助提前。

+0

它一直在*尝试*,但它不是必需的。事实上,这是一个非常糟糕的做法。只需使用一个集合。 – delnan 2011-12-30 17:54:49

+0

请更正示例代码中的缩进。你知道,这在Python中很重要。 – 2011-12-30 17:56:48

+0

感谢大家的回复,并感谢我对Python的新手知识。 – user1123221 2011-12-30 18:20:18

回答

3

而不是使用一个新的变量为每一位客户则可以将对象存储在一个Python字典:

d = dict() 

for record in result: 
    objectname = 'Customer' + str(record[0]) 
    customername = str(record[1]) 
    d[objectname] = Customer(customername) 

print d 

我只是could'nt帮助我的自我书面方式存储在字典对象的示例一些代码(比我想要做的更多)。这就像上瘾。无论如何,我不会使用对象进行这种工作。我可能会使用一个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() 
+0

那么,与我一样使用相同解决方案的Rotem对词典有更好的名称 - “客户”。否则它们几乎是一样的:) – 2011-12-30 18:02:40

+0

对不起,在写你的时候开始写它,这显然是相同的答案。 – Rotem 2011-12-30 18:10:53

+0

感谢您的回复和您的时间,我可以看到您正在创建一本字典,但它看起来像您将对象分配给字典,反对将值添加到字典中,然后将其指定给对象。 这显然是做到这一点的正确方法,但它似乎与我不直观。:S – user1123221 2011-12-30 18:15:56

1

你需要的是一本字典:

customers = {} 
for record in result: 
    objectname = 'Customer' + str(record[0]) 
    customers[customername] = Customer(str(record[1])) #assignment to dictionary 
+0

感谢您的回复和您的时间,我可以看到您正在创建一本字典,但它看起来像您将对象分配给字典,反对在字典中添加一个值,然后将其作为对象。这显然是做到这一点的正确方法,但对我来说似乎是违反直觉的。:S – user1123221 2011-12-30 18:16:40

+0

我无法想象它以何种方式似乎更复杂或反直觉,但是对于每个他自己我猜.. – Rotem 2011-12-30 19:10:32

2

它一般是没有多大用处具有动态生成的变量名称。我肯定会建议是这样的Niclas'的答案,而不是,但如果你知道这是你想要的这里是你如何能做到这一点:

for record in result: 
    objectname = 'Customer' + str(record[0]) 
    print objectname 
    customername = str(record[1]) 
    exec '%s = Customer(%r)' % (customername, customername) 

这将导致变量Customer1Customer2被添加到最里面的范围,酷似如果你执行了下面几行:

Customer1 = Customer('Customer1') 
Customer2 = Customer('Customer2') 

在做这种方式,你需要确保customernamevalid Python identifier

+0

感谢您的帮助! – user1123221 2011-12-30 18:17:17

+0

这有助于我更好地理解总体思路! – user1123221 2011-12-30 18:35:57

相关问题