2011-12-30 94 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