attribute assignment effects all class instances

Jeff Shannon jeff at ccvcorp.com
Thu Sep 9 22:14:20 EDT 2004


And since others have already told you *why* you're having a problem, 
I'll limit myself to showing you one possible way around that problem.

    class Node:
        def __init__(self, ... , adjacent = None):
            # ....
            if adjacent is None:
                adjacent = [None, None, None, None]
            self.adjacent = adjacent

This way, if you don't specify adjacent, you create a *new* list of Nones, which thus won't be shared by other instances of Node.  If you *do* specify adjacent, then you get what you specified (and it's up to you to make sure that it's not shared with any other Nodes).

Jeff Shannon
Technician/Programmer
Credit International





More information about the Python-list mailing list