Stuck newbie

Diez B. Roggisch deets_noospaam at web.de
Tue Dec 2 19:25:13 CET 2003


> If that is correct, what I don't understand is how to reference and
> modify elements of an individual object.  For example, I want to do
> things like (in pseudocode):

The nice thing is - your pseudocode is quite close python-code ;)
 
> namelist.append( new_record )
> ....
> namelist[ index ].flags = 99
> namelist[ index ].list1.append( 100 )
> 

namelist = []
new_record = [0, [], []]
namelist.append(new_record)
namelist[-1][0] = 99
namelist[-1][1].append(100)

However, if you prefer to access the records fields using names, you can go
with a dicionary:

new_record = {'flags': 0, 'list1':[], 'list2':[]}

Then access is like this

namelist[-1]['flags'] = 99
namelist[-1]['list1'].append(100)

The last approach would be to create a class new_record:

class new_record:
  def __init__(self):
    self.flags = 0
    self.list1 = []
    self.list2 = []

Then your code above should work.

Diez




More information about the Python-list mailing list