2013-05-05 12 views
5
globalList = [] 
class MyList: 
    def __init__(self): 
     self._myList = [1, 2, 3] 

    @property 
    def myList(self): 
     return self._myList + globalList 
    @myList.setter 
    def myList(self, val): 
     self._myList = val 

mL1 = MyList() 
print("myList: ", mL1.myList) 
mL1.myList.append(4) 
print("after appending a 4, myList: ", mL1.myList) 
mL1.myList.extend([5,6,"eight","IX"]) 
print("after extend, myList: ", mL1.myList) 

Risultato:Python proprietà decorare setter con la lista

myList: [1, 2, 3] 
after appending a 4, myList: [1, 2, 3] 
after extend, myList: [1, 2, 3] 

Il problema che sto affrontando è che mL1.myList.append (4) e mL1.myList.extend ([5,6, otto " "," IX "]) non modificare l'attributo _myList nell'oggetto mL1. Come potrei fare per risolvere il problema?

+0

Potrebbe risolvere il rientro? Non sono sicuro di dove sia destinata la seconda fiction. –

+0

Non mi è chiaro quale comportamento ti aspetteresti. Dovrebbe 'append()' aggiungere a '_myList' (ovvero i valori non verranno aggiunti alla fine come ci si aspetterebbe) o a' globalList'? Ad ogni modo, è abbastanza chiaro che questo non funzionerebbe in quanto il valore restituito è una nuova lista prodotta concatenando altri due elenchi. Estendere ciò non cambierà in qualche modo. –

+0

append() dovrebbe aggiungersi a _myList, e così extender() – user14042

risposta

4

definisco un metodo append() e un metodo extend() per l'oggetto di classe. Aggiunge rispettivamente a myList membri e estende myList membri.

global globalList 
globalList = [] 
class MyList(): 
    def __init__(self): 
     self._myList = [1, 2, 3] 

    @property 
    def myList(self): 
     return self._myList + globalList 
    @myList.setter 
    def myList(self, val): 
     self._myList = val 

    def append(self, val): 
     self.myList = self.myList + [val] 
     return self.myList 

    def extend(self, val): 
     return self.myList.extend(val) 


mL1 = MyList() 
print("myList: ", mL1.myList) 
mL1.append(4) 
print("after appending a 4, myList: ", mL1.myList) 
mL1.myList.extend([5,6,"eight","IX"]) 
print("after extend, myList: ", mL1.myList) 

risultato è

>>> 
('myList: ', [1, 2, 3]) 
('after appending a 4, myList: ', [1, 2, 3, 4]) 
('after extend, myList: ', [1, 2, 3, 4, 5, 6, 'eight', 'IX']) 
+0

Grazie per il suggerimento! – user14042

+1

Il codice è molto rotto :) MyList non deriva dall'oggetto, che interrompe la proprietà setter, quindi dopo l'assegnazione a myList sarà un elenco semplice. Una volta risolto, append() aggiungerà globalList a _mylist per ogni chiamata. Anche l'estensione di implementazione è interrotta, dovrebbe estendere _myList, altrimenti estenderebbe solo la lista temporanea restituita dalla proprietà. Infine, l'esempio chiama mL1.myList.extend anziché mL1.extend. – user1346466

2

avrei sottoclasse list e sovrascrivere alcuni metodi:

import itertools 

class ExtendedList(list): 
    def __init__(self, other=None): 
     self.other = other or [] 

    def __len__(self): 
     return list.__len__(self) + len(self.other) 

    def __iter__(self): 
     return itertools.chain(list.__iter__(self), iter(self.other)) 

    def __getitem__(self, index): 
     l = list.__len__(self) 

     if index > l: 
      return self.other[index - l] 
     else: 
      return list.__getitem__(self, index) 

dovrebbe funzionare con quasi tutto:

In [9]: x = ExtendedList([1, 2, 3]) 

In [10]: x 
Out[10]: [1, 2, 3] 

In [11]: x.append(9) 

In [12]: x 
Out[12]: [9, 1, 2, 3] 

In [13]: x.extend([19, 20]) 

In [14]: x 
Out[14]: [9, 19, 20, 1, 2, 3] 

In [15]: sum(x) 
Out[15]: 54 
+0

Ciao grazie per la risposta, è interessante vedere come la si implementa. Spero non ti dispiaccia che ti pongo una semplice domanda: self.other = other or [], qui viene utilizzata la valutazione di cortocircuito? quindi se l'altro parametro non viene fornito, allora sarà una lista vuota? – user14042

+0

@ user14042: Sì, se 'other' è truey (non' None' o una lista vuota in questo caso), verrà restituito. In caso contrario, l'altro lato sarà. – Blender

Problemi correlati