referring from within containers objects

Sandy Norton sandskyfly at
Mon Mar 10 11:11:48 CET 2003


This is just an idea that occurred to me while working with
dictionaries in which the values of certain keys were composed of the
values of other keys and I found that the syntax for solving that
problem wasn't as pretty as I would have liked. 

Here's an example:

d = {
  'fname' : 'Nit',
  'lname' : 'Wit'

Now I have to do this:

d['fullname'] = d['fname'] + ' ' + d['lname']

if I want to reuse the keys/values within the dict. This forces me to
break out of the dictionary definition, which I don't like doing for
some reason.

Now what if I could do something like this: 

d2 = {
    'fname' : 'Nit',
    'lname' : 'Wit',
    'fullname' : _['fname'] + ' ' + _['lname']

where '_' refers to the containing dict.

This can be extended to lists:

lst = [ 'Nit', 'Wit', _[0] + ' ' + _[1] ]


More information about the Python-list mailing list