[Tutor] Self referencing within a dictionary

Alan Gauld alan.gauld at freenet.co.uk
Sun Apr 3 17:05:17 CEST 2005


> foo = {'a':1, 'b':2, 'c':foo['a']}

TRy

foo = {'a':1, 'b':2}
foo['c'] = foo['a']

The only reasobn it ddidn't work before was that
foo didn't exist at the time you tried to add the reference.

foo['c'] is just a reference to whatever foo['a'] references.

foo['d'] = foo

is a tad more interesting however.

especially if you do

foo['d']['d']['b'] = 42

As a mental excercise...
Now what does foo look like? :-)


> I know that as above doesn't work, but was just wondering if it's
> possible, and if it's a
> Bad Thing?

It's possible and may be a good thing depending on what you are trying
to do. It certainly shouldn't be a regular thing!

Alan G.



More information about the Tutor mailing list