Atomic Power

"""
Lets represent Atom objects using syntax associated
with the namedtuple type (in collections)
"""

class Atom:
  
  def __init__(self, protons, mass, symbol, long_name):
    """An Atom is born..."""
    self.protons = protons
    self.mass = mass
    self.symbol = symbol
    self.long_name = long_name
    
  def __getitem__(self, idx):  # access by integer
    """replicate tuple behavior w/r to indexing"""
    if idx == 0:
      return self.protons
    elif idx == 1:
      return self.mass
    elif idx == 2:
      return self.symbol
    elif idx == 3:
      return self.long_name
    else:
      raise IndexError("tuple index out of range")
      
  def __repr__(self):
    """imitate namedtuple __repr__"""
    fmt = "Atom(protons={protons}, mass={mass}, symbol='{symbol}', long_name='{long_name}')"
    return fmt.format(**self.__dict__) # named args
    
# testing

# a dict with all the data for Hydrogen
args = {"protons":1, "mass":1.008, "symbol":"H", "long_name":"Hydrogen"}

the_atom = Atom(**args) # explode into named args
print(the_atom)

the_atom = Atom(*args.values()) # explode into positional args
print(the_atom)

print("-"*20)
about = """The {it.long_name} atom has 
{it.protons} proton(s) and a mass of {it.mass}"""
print(about.format(it=the_atom))

print("-"*20)
about = """The {it[3]} atom has 
{it[0]} proton(s) and a mass of {it[1]}"""
print(about.format(it=the_atom))