This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Slicing changes item-attributes of an instance of a UserList
Type: Stage:
Components: Interpreter Core Versions: Python 2.3
process
Status: closed Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: hflori, jepler, rhettinger
Priority: normal Keywords:

Created on 2004-04-19 07:23 by hflori, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Messages (4)
msg20541 - (view) Author: Hans Flori (hflori) Date: 2004-04-19 07:23
Hi, Py!

I got a strange effect on Items of an instance of a 
UserList-inherited class. In some cases, slicing the
list changed the attribute of the items.

I can not see the mistake in my code and I think that
the interpreter is not correct here.

ciaou
flo


The code - included some output to show the effect - is:

from UserList import UserList

#----------------------------------------------------------------------
# The items class
#----------------------------------------------------------------------
class Player:
    def __init__(self, name):
        self.xname = name

    def __repr__(self):
        return repr(self.__dict__)

#----------------------------------------------------------------------
# The lists class
#----------------------------------------------------------------------
class Team(UserList):
    def __init__(self, names=None):
        if names:
            UserList.__init__(self,
                [Player(name) for name in names])
        else:
            UserList.__init__()



#----------------------------------------------------------------------
# The Test
#----------------------------------------------------------------------
if __name__ == '__main__':
   
   tnames = ['Huber','Maier','Mueller']
   crew = Team(tnames)

   print '\nThis is ok'
   l =  [(i,) for i in crew]
   for i in l:  print i

   # This is ok
   # ({'xname': 'Huber'},)
   # ({'xname': 'Maier'},)
   # ({'xname': 'Mueller'},)

   print '\nThis again is ok'
   l =  [(crew[i],) for i in range(len(crew))]
   for i in l: print i

   # This again is ok
   # ({'xname': 'Huber'},)
   # ({'xname': 'Maier'},)
   # ({'xname': 'Mueller'},)

   print '\n **** This is    w r o n g  *******'
   l =  [(i,) for i in crew[:3]]
   for i in l:  print i

   #  **** This is    w r o n g  *******
   # ({'xname': {'xname': 'Huber'}},)
   # ({'xname': {'xname': 'Maier'}},)
   # ({'xname': {'xname': 'Mueller'}},)

   crew = UserList([Player(name) for name in tnames])   

   print '\n**** Oh! This is ok!  *******'
   l =  [(i,) for i in crew[:3]]
   for i in l:  print i

   # **** Oh! This is ok!  *******
   # ({'xname': 'Huber'},)
   # ({'xname': 'Maier'},)
   # ({'xname': 'Mueller'},)


msg20542 - (view) Author: Jeff Epler (jepler) Date: 2004-04-22 18:20
Logged In: YES 
user_id=2772

The slicing operation calls your class's __init__ to
construct the new list.
If your __init__ performs some operation on the given items
and passes it to UserList.__init__, that's exactly as expected.

Here's a simpler example---each time the list is sliced,
X.__init__ increases all the items in the new list by 1:
from UserList import UserList
 
class X(UserList):
. . def __init__(self, l = []):
. . . . UserList.__init__(self, [i+1 for i in l])
        
x = X([1,2,3])
print x, x[:], x[:][:]
# prints [2, 3, 4] [3, 4, 5] [4, 5, 6]
msg20543 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2004-04-22 20:50
Logged In: YES 
user_id=80475

Hans, if you understand and agree, please close this bug.
msg20544 - (view) Author: Hans Flori (hflori) Date: 2004-04-22 21:16
Logged In: YES 
user_id=1024163

hello rhettinger,

thank you for your answer. UserLists methods have been a
blind spot for me when I tried to find the error. 

History
Date User Action Args
2022-04-11 14:56:03adminsetgithub: 40168
2004-04-19 07:23:47hfloricreate