2010-09-08 3 views
1

Je ne comprends pas pourquoi la nouvelle instance de la classe TypeList n'a pas d'attribut my_type.Python: problème avec deepcopy (ing) une classe TypedList héritant de list et overriding Ajout

t.i.a. pour toute aide

Voici mon code:

import copy 

class TypedList(list): 
    def __init__(self, typeof, iterable=''): 
     """ 
     Initialize the typed list. 

     Examples: 
     tmp = TypedList(str, 'foobar') # OK 
     tmp = TypedList('str', 'foobar') # FAIL! 
     """ 
     if not issubclass(type(typeof), object): 
      raise TypeError('typeof must inherit from object') 

     if type(typeof) is not type: 
      raise TypeError('typeof, argument must be a python object type.\n' 
       'Not a string stating the type of python object.\n' 
       'Example: TypedList(str), not Typedlist("str")') 

     self.my_type = typeof 

     for item in iterable: 
      if type(item) != self.my_type: 
       raise TypeError('%s is of type %s; it should be type %s' % 
        (item, type(item), self.my_type)) 

     super(TypedList, self).__init__(iterable) 

    def append(self, item): 
     """ 
     Append an item to the typed list. 
     """ 
     if type(item) != self.my_type: 
      raise TypeError('item must be of type %s' % self.my_type) 

     return super(TypedList, self).append(item) 

if __name__ == '__main__': 
    foo = TypedList(str, 'test') 
    bar = copy.deepcopy(foo) 

exécution retourne cette sortie:

$ python tl.py 

Traceback (most recent call last): 

    File "tl.py", line 53, in <module> 

    bar = copy.deepcopy(foo) 

    File "/usr/lib/python2.6/copy.py", line 189, in deepcopy 

    y = _reconstruct(x, rv, 1, memo) 

    File "/usr/lib/python2.6/copy.py", line 329, in _reconstruct 

    y.append(item) 

    File "tl.py", line 46, in append 

    if type(item) != self.my_type: 

AttributeError: 'TypedList' object has no attribute 'my_type' 
+0

eww, désolé pour le formatage laide. – user442585

Répondre

0

Je viens de tester votre script avec Python 2.5 et Python 2.7 et il fonctionne:

import copy 

class TypedList(list): 

    def __init__(self, typeof, iterable=''): 
    """ Initialize the typed list. 

    Examples: 
    tmp = TypedList(str, 'foobar') # OK 
    tmp = TypedList('str', 'foobar') # FAIL! 
    """ 
    if not issubclass(type(typeof), object): 
     raise TypeError('typeof must inherit from object') 

    if type(typeof) is not type: 
     raise TypeError('typeof, argument must be a python object type.\n' 
      'Not a string stating the type of python object.\n' 
      'Example: TypedList(str), not Typedlist("str")') 

    self.my_type = typeof 

    for item in iterable: 
     if type(item) != self.my_type: 
      raise TypeError('%s is of type %s; it should be type %s' % 
       (item, type(item), self.my_type)) 

    super(TypedList, self).__init__(iterable) 

def append(self, item): 
    """ 
    Append an item to the typed list. 
    """ 
    if type(item) != self.my_type: 
     raise TypeError('item must be of type %s' % self.my_type) 

    return super(TypedList, self).append(item) 

if __name__ == '__main__': 
    foo = TypedList(str, 'test') 
    bar = copy.deepcopy(foo) 
    assert foo.my_type is bar.my_type 
+0

Strange Je viens de télécharger et de compiler la source de pyhton 2.7. J'ai toujours ce problème. – user442585

Questions connexes