2012-08-29 167 views
0

我是新手到Python,我認爲類似的問題已經被問(包括本:Can you use a string to instantiate a class in python?),但我不明白的答案或如何應用它們。訪問實例變量,而不是實例方法在Python

我想創建一個類的多個實例,使用我將在列表中調用「實例名稱」。

這裏是什麼,我試圖做一個例子:

class InstanceNames(): 
    def __init__(self): 
     self.names = ['inst1', 'inst2', 'inst3'] 

class DoSomething(): 
    instances = [] 
    def __init__(self): 
     DoSomething.instances.append(self) 

instance_names = InstanceNames() 
for x in range(len(instance_names.names)): 
    print x 
    # following line not working at creating instances of DoSomething 
    instance_names.names[x] = DoSomething() 

print DoSomething.instances 

我改變了列表循環,現在我得到以下輸出:

0 
1 
2 
[<__main__.DoSomething instance at 0x10cedc3f8>, <__main__.DoSomething instance at 0x10cedc440>, <__main__.DoSomething instance at 0x10cedc488>] 

做的工作?我很困惑,我不確定。

好的。這是一些醜陋的代碼,但這裏是我現在有:

class InstanceNames(): 
    def __init__(self): 
     self.i_names = {'inst1': None, 'inst2': None, 'inst3': None} 
     self.o_names = ['foo', 'bar', 'baz'] 

class DoSomething(): 
    instances = [] 
    def __init__(self, blah_blah): 
     DoSomething.instances.append(self) 
     self.name = blah_blah 

    def testy(self, a): 
     a = a * 2 

instance_names = InstanceNames() 

for x in range(len(instance_names.i_names)): 
    print x 
    instance_names.i_names[x] = DoSomething(instance_names.o_names[x]) 

print "\n" 

print DoSomething.instances 

print "\n" 

for y in DoSomething.instances: 
    print y.name 
    print y.testy(4) 
    print "\n" 

這裏是我的輸出:

0 
1 
2 


[<__main__.DoSomething instance at 0x10dc6c560>, <__main__.DoSomething instance at 0x10dc6c5a8>, <__main__.DoSomething instance at 0x10dc6c5f0>] 


foo 
None 


bar 
None 


baz 
None 

爲什麼是「名」可變印刷,但「暴躁」的方法是不是?

+2

這不是列表的工作方式。 –

+0

謝謝。更改列表的for循環。仍然沒有得到我想要的。會愛你的幫助。謝謝。 – dwstein

+1

'len(self.names)'應該是'len(instance_names.names)' –

回答

0

這是什麼意思or x

self.names不存在的範圍,使用instance_names.names代替。

1

您似乎在問:「我如何接收字符串'inst1'並創建一個名爲'inst1'的變量」。

答案是你不想這樣做。相反,創建一個字典或列表將字符串映射到有問題的對象。有關示例,請參閱this question

(如果這不是你問,請澄清你的問題。)

1

這並不是說我有什麼想法,你真正要做的,以滿足您的特定代碼,表僅僅是一個集合值。你對它的看法是一個字典,它是一個關鍵和價值之間的聯繫。

爲了使您的工作例如,你可以使用的字典:

class InstanceNames(): 
    def __init__(self): 
     self.names = {'inst1': None, 'inst2': None, 'inst3': None} 

現在,這將允許下面的表達式成功:

instance_names.names[x] = DoSomething() 

...因爲names現在的字典和您正在訪問一個密鑰併爲其分配一個值。

我再次聲明,我不知道這段代碼試圖做什麼...有這種感覺可能不是很好......但沒有采取判斷它的角度。

1

instance_names.nameslist需要數字作爲指標。事實上,你發佈的TypeError也是一樣的。

但是,您想使用instance_names.names[x]中的字符串設置元素 - 其中x是一個字符串。列表不允許這樣做,您需要使用dictionary

有幾種方法來解決問題:

  1. 您可以使用字典instance_names.names擺在首位。然而,您必須使用一個保留的對象,例如None作爲尚未創建的實例的佔位符:'self.names = {'inst1':None,'inst2':None,'inst3':None} , and you must iterate through the keys of the dictionary: for example in instance_names.names.keys():`

  2. 您可以爲實例使用單獨的字典並在循環中設置其內容:self.instances = {}instance_names.instances[x] = ...

進一步閱讀關於Python的數據類型,我建議Dive Into Python

1

您可以使用type動態創建類:

type_names = ["Class1", "Class2", "Class3"] 
storage = {} 
for t in type_names: 
    storage[t] = type(t, (object,), {"your": "instance", "attributes": "here"}) 

for type_name in storage: 
    print type_name, "=>", storage[type_name] 

另外,也可以使用collections.namedtuple生成輕量級類,如果你真正需要的是一堆屬性。

什麼你目前已經被創建DoSomething類的三個實例,並在InstanceNames類的names屬性與DoSomething這三種情況下替換字符串值(不使用)。