[Tutor] Populating a list with object to be called by a class

eryksun eryksun at gmail.com
Thu Sep 20 19:18:58 CEST 2012


On Thu, Sep 20, 2012 at 12:57 PM, eryksun <eryksun at gmail.com> wrote:
>
> I forgot the obligatory warning about class variables. The subclass
> gets a shallow copy of the parent class namespace. The in-place
  ^^^^^^^^^^^^^^^^^^^
>     >>> Y.data += [1]
>     >>> X.data  # modified parent, too
>     [1]
>     >>> Y.data = Y.data + [2]  # creates new list

To clarify, it's not an actual dict.copy in the subclass. In fact, the
atrribute is initially only in the parent __dict__, where it's found
by object.__getattribute__. In the 2nd example, Y.data on the
right-hand side is actually found in the parent class X, then the
expression creates a new list which is stored back to Y.


More information about the Tutor mailing list