Complex datastructures in Python

David N. Welton davidw at linuxcare.com
Wed Jun 28 05:43:17 EDT 2000


Eric Lee Green <eric at estinc.com> writes:

> Greg Gallagher wrote:

> > I'm a little disheartened here... I know how to do this in Perl,
> > but I'm lost in Python and searching for 'complex data structures'
> > is landing me nowhere.  Basically, I want a dictionary whosevalues
> > are lists of dictionaries.  Simple.  I want to basically loop
> > through some data and so something like this:

> You may want to simply this by using classes. For example, here's a class
> "IP_Address":

> class IP_Address:
>     def __init__(self,ip,hostname,mac_address):
>          self.ip=ip
>          self.hostname=hostname
>          self.mac_address=mac_address

> Then:
>       new_forward=IP_Address(ip_address,hostname,mac_address)

> Then:

>       masq_table['forward'].append(new_forward)

> then later:

>    for ip in masq_table['forward']:
>        ...do something here...

I think this is the best solution, in the long run.  I used to use
Perl a lot, and did all the list of list of hashes of lists, and so
on.  But using something like a struct will improve the long-term
readability, maintainability and extensibility of the code, IMO.  

-- 
David N. Welton, Responsabile Progetti Open Source, Linuxcare Italia spa
tel +39.049.8043411 fax +39.049.8043412 cel +39.348.2879508
davidw at linuxcare.com, http://www.linuxcare.com/
Linuxcare. Support for the revolution.



More information about the Python-list mailing list