Starting Code and Objectives#

Starting File: 00_base_molecule.py

This chapter will start from the 00_base_molecule.py and end on the 00_base_molecule.py. No modifications will be made in this chapter, we’re simply looking at the structure.

This chapter will cover the basic file we will be manipulating, what it does, and what we will be doing to it. The commands we run will be very simple, but we will be manipulating the main class shown below as we build out our features and cover concepts.

Base Class: A Simple Molecule#

Below, we have the code that we’re going to be manipulating with a main class we’ve called Molecule

class Molecule:
    def __init__(self, name, charge, symbols, coordinates):
        self.name = name
        self.charge = charge
        self.symbols = symbols
        self.coordinates = coordinates
        self.num_atom = len(symbols)

    def __str__(self):
        return f"name: {self.name}\ncharge: {self.charge}\nsymbols: {self.symbols}"

What is Molecule? It is a very simplistic representation of a molecule that you might choose to write as you go about developing your own code especially if you’re in the molecular sciences. It’s unlikely that you will necessarily need to write this on your own as there are many external libraries you could use but for that purpose of this workshop, we’re going to be building this out.

The base Molecule class that simply takes as its initialization an arbitrary name, a charge, symbols for the elements, and coordinates. Instanciation then assigns each of these arguments to a corresponding attribute of the same name. Finally, then we also add an extra attribute to provide a number of atoms.

Let’s put our Molecule class to work and create a very simple Water representation.

water = Molecule("water", 0.0, ["H", "H", "O"], [0, 0, 0])

The water we’ve specified has no charge; has elements H, H, and O; and simple coordinates centered at (0,0,0 in a 3D space.

You may note that the coordinates are not actually sufficient to specify 3, three-dimensional locations, but instead define a one-dimensional location, so this could be center of mass coordinates this but this certainly isn’t atomic coordinates. We’ll come back to that later and instead just print out our water.

print(water)
name: water
charge: 0.0
symbols: ['H', 'H', 'O']

Problems of Types#

Everything was assigned correctly and dutifully represented through the __str__ method we wrote into the class. However, you might notice several things about this that aren’t necessarily the most clear: what type of data does this class accept?

Python is what is called a “Duck Typing Language,” so arguments and variables that are assigned from those arguments just assume or adopt the type given to them without any regard if they can be syntactically used correctly. If types were enforced natively in Python, it would be called a “Strict Typing Language.” Duck typing is a very powerful tool in that you can code quicker and be more flexible, but it does have downsides, especially in the scientific computing world where data and their types tend to require rigor.

If we wanted to provide some useful information not only you the developer, some other developer, or end users right inside the code, separate from documentation strings, we can use something called “Type Hints”