More assignment/copy novice confusion
Laurent Pointal
laurent.pointal at laposte.net
Sun Dec 2 13:48:09 EST 2001
More information about the Python-list mailing list
Sun Dec 2 13:48:09 EST 2001
- Previous message (by thread): More assignment/copy novice confusion
- Next message (by thread): More assignment/copy novice confusion
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
[posted and mailed] "Arthur Siegel" <ajs at ix.netcom.com> wrote in news:mailman.1007306883.27068.python-list at python.org: > Given a class: > >>>> class p: > def __init__(self,a): > self.a=a > Then - >>>> m=p([1,2,3]) >>>> r=m >>>> m.a=([1,2,4]) >>>> m.a > [1, 2, 4] >>>> r.a > [1, 2, 4] > > clear enough. > But - > >>>> m=p([1,2,3]) >>>> r=p([]) Here r and m reference two **different** objects of class p, with their own attributes. Try "r is m", and it will reply 0. >>>> r.a=m.a Here m.a and r.a, which are attributes of different objects, reference the same list. >>>> m.a=[1,2,4] And now m.a reference another list. >>>> m.a > [1, 2, 4] >>>> r.a > [1, 2, 3] You have stored orinal m.a list into r.a and then replaced m.a list, so you see the original in r.a and the new in m.a. Its normal. > And - > >>>> m=p([1,2,3]) >>>> r=m Here r and m reference the SAME object, so modifying m data is like modifying r data. Try "r is m", and it will reply 1. >>>> r.a=m.a This is not needed, as r and m are the same object, r.a and m.a are the same. >>>> m.a=[1,2,4] And again, m.a is r.a as m is r. >>>> m.a > [1, 2, 4] >>>> r.a > [1, 2, 4] Its normal. > I know this isn't the tutorial list. > > But - on the theory that my confusion > on these kinds of issues is widely shared > among novices - I thought it worth bringing > up for discussion, clarification. Hope my comments help. > Is anything here affected by the new style classes? A-priori, no. > > Art A+ Laurent.
- Previous message (by thread): More assignment/copy novice confusion
- Next message (by thread): More assignment/copy novice confusion
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
More information about the Python-list mailing list