data structure design question
Felix Thibault
felixt at dicksonstreet.com
Sun Feb 13 02:37:47 EST 2000
At 08:07 2/12/00 +0200, Moshe Zadka wrote:
>On Fri, 11 Feb 2000, Felix Thibault wrote:
<snip>
>
>Hmmm...why not have a generic Atom:
>
>h1 = Atom('H', 1)
>
>(actually, the 1 is superfluous: you can have a table mapping name to
>number of electrons on the outer level (is that it?))
>
I was thinking of having a generic class that could initialize with
some optional parameters for stuff like charge or isotope that could
vary from atom to atom, with default and constant values in a table,
like this:
PeriodicTable = { 'H':{'name':'hydrogen', 'valence_electrons': 1,\
'mass': 1.0078, 'atomic_number':1}} #more...
class Element:
#set count for each element to zero
indexes = {}
for key in PeriodicTable.keys():
indexes[key] = 0
del key
def __repr__(self):
return self.symbol + `self.index`
def __init__(self, symbol, valence_electrons=None):
self.symbol = symbol
Element.indexes[symbol] = Element.indexes[symbol] + 1
self.index = Element.indexes[symbol]
if valence_electrons is None:
self.valence_electrons =
PeriodicTable[symbol]['valence_electrons']
else:
self.valence_electrons = valence_electrons
self.mass = PeriodicTable[symbol]['mass']
self.atomic_number = PeriodicTable[symbol]['atomic_number']
#more...
def reset():
"""reset() set all element counts back to zero"""
for key in PeriodicTable.keys():
Element.indexes[key] = 0
def H():
return Element('H')
class Molecule:
def __init__(self, bonddict):
mass = 0
atoms = bonddict.keys()
for atom in atoms:
mass = mass + atom.mass
self.mass = mass
self.bonds = bonddict
self.atoms = atoms
h1, h2 = H(), H()
hydrogen_molecule = Molecule({h1:[h2], h2:[h1]})
<snip>
>> The dictionary method seems more condensed and readable, so I
>> am preferring it right now, even though I would have to make a
>> new instance for every atom of an element
>
>That's a good thing. Every atom *is* a new instance of the atom class.
>
Of course! I won't be stingy with them, then.
Thanks!
Felix
>--
>Moshe Zadka <mzadka at geocities.com>.
>INTERNET: Learn what you know.
>Share what you don't.
>
>
>--
>http://www.python.org/mailman/listinfo/python-list
>
>
More information about the Python-list
mailing list