18. Using classes in python
Oct 8, 2023
Classes are a super helpful data type in python. We can create objects that share similar information types. This video shows an example of classes where we create members of Bravais lattice and store relevant information like crystal system or centering as fields within the class. We can then create various instances of the class for different elements and store their information in a compact, useful way. We can also define custom methods and use a helpful initialization function as well as write help documentation for the class so we remember what the class was later ;)
00:00 what are classes?
3:30 help and initialization
9:08 custom methods
11:38 example of classes to classify Bravias lattices
Today we're going to talk about classes classes are a type of variable or type of object
I should say in Python that are really useful
Think of them sort of like a cookie cutter a cookie cutter can make from the dough the same sort of cookie each time
And each cookie now has the same shape and maybe you're gonna decorate it the same way
Classes do the same thing with objects. They create the same type of objects objects that share the same type of field
So think of students in a classroom students in a classroom. They're all gonna have a first name
They're all gonna have a last name. They're all gonna have like a school number an email address a grade, right?
So because all those share common attributes, we're gonna use the class function to define them
Okay. Now in the example, we're gonna do today. We're gonna tie it to material science
Let's go ahead and make a class called Bravais lattice. You do that by using the keyword class
It turns purple right and then we're gonna do Bravais. We're gonna do capital Bravais underscore lattice
The convention is to use capital letters on the first words of a class, right? So we're gonna go ahead and create this class
Colon and the first thing that we should do is well, we're gonna put stuff inside of here later for now
Let's just put pass because if you don't put
Anything it's gonna cause an error. So for now, we've just created a class called Bravais lattice and there's nothing inside of it
So we can create members of this class just like there's like members of the cookies you make with a cookie cutter
We can create members of this class. We call them instances. So an instance of this class
Let's pick a material like copper right we can say that see you copper is a member of Bravais lattice
We can do that just by doing typing Bravais underscore lattice. And since it's a class we need to put
It's one of those at the end
Okay, and voila when we run this now we now if we go to our variable Explorer
We see that we now have our variable copper. It is of the type Bravais lattice the type of class we created here
Okay. Okay. Now we've made copper as a member of this
So what do you use these for one of the great things about?
Classes is again these associated information tied with them
So let's say every material that is in a member of the 14 Bravais lattices
It's gonna have a crystal structure and it's gonna have like a lattice parameter
It's gonna have you know, the angles that we can think of so right now we could say like copper dot
lattice parameter
The lattice parameter of copper is equal to what three point six one five angstrom
Okay, so when we run that now if we see this nothing's changed over here
But if you select this you'll notice now that ah sure enough in the copper class in copper
Which is an instance of the Bravais lattice class. It now has this information called lattice parameters with the value stored
We call these fields right these pieces of information
Associated with the class are called fields and the convention is to use lowercase letters for those with an underscore
Separating them right and you can do as many as you want
You could do like copper color if you wanted right and you could say, you know, its color is, you know, copper colored
I don't know how you'd say that, right?
So and then sure enough if you open it up, you would see that it has these different fields
It now has the the color field. Okay, as well as the lattice parameter field now, you might say well sure
But couldn't we have just said like that the copper lattice?
Parameter equals
3.615 like just make a variable right store it as a float and name the variable that you can
But this is a better way to hierarchically organize your data and it has some other big advantages
Here's three big advantages for why we want to use classes. We can do something called initialization, which is really great
There's custom methods that we can write for them and then there's a help file, right? So let's go ahead and do that
First let's show you how to do the help file after you declare your class
We're gonna do what's called a doc string you do a doc string by doing three
apostrophes or quotation marks in a row and then you can type some stuff like here is
Where we write the help right and this shows up when people ask for help on this class
It will tell them this so we actually want to say something a little more helpful than that. Maybe we could say like
This class defines all the structural
Info for a material. Okay, and that way we know what this class is actually doing. Great. Okay now
Let's so the first so there's help. There's also initialization and methods. Let's do initialization next, right?
So we're going to define a using a this special syntax underscore and underscore. I and IT that stands for initialization, right?
okay, and
So when we initialize it when you initialize a new
Instance of a class the first thing we're gonna initialize it is itself, right? So
Self and then you can send it some information. So for the first time when we initialize copper
We didn't send it any information, but we could have sent it information. Like let's say we wanted to send it the crystal structure
Or the crystal system, which is cubic and the centering which is face centered
So let's go ahead and say that we're gonna be expecting those. We're gonna be expecting the crystal system and
The centering okay. Now once you have those we can go ahead and say that self dot crystal system
equals
We'll say that is equal to the crystal system and that
self dot
Centering is going to be equal to centering now heads up here the word on the right centering is a variable, right?
But this over here is a field they can share the same name or they could have been different
Like if instead of centering if we wanted to call it like center type or centering type, that would be okay
That would now be the name of our field. Okay
So now if we try and run this what will happen if we try and run this right now
Then we get this error see that we get this type error. It says hey in your initialization
There's two things missing crystal system and centering are required arguments when you do this
So it's yelling at us saying those were supposed to be there great
Well, we can add them when we and when we declare this copper as a member as an instance of the class Bravais lattice
We can always go ahead and add that it's a member of the cubic crystal system, right?
And then for centering it is face centered. Okay, so now when we run it we can go ahead and open up our memory our
copper class map instance here and we see that it's got
Check it out
it's got this centering type field and it says that it is face for the centering type and
Under crystal system it is cubic. Okay, so this is already getting more useful, right?
It's it's a it's a defined way to organize your variables each time
But now you can do some really cool functions if you wanted to you could say like self dot
crystal
Structure is now equal to you could say that it's equal to
crystal
underscore system
plus
Centering let's go ahead and run that now. We pull this up. You'll see that the crystal structure is
cubic face
Okay, maybe you wanted to add like one of these in there some some spacing and stuff like that
Or you can make it, you know do some other things, but it's already getting more useful for us
Okay, and if you declare something else, right, let's declare another variable over here. Let's call it magnesium
We can do our Bravais lattice there and it's different. It is hexagonal
Hexagonal and it's centering is base center. Yeah, it's base centered. I think
Hexagonal closed packed. Okay, so we go ahead and run that now now we've got two Bravais lattice type objects, right?
Both instances of this class and if you look at magnesium check it out
We never defined its lattice parameter, but it doesn't matter
Right if we want to add it later, that's no problem. But right now by default all it has is
Crystal system centering type and crystal structure and there they are defined like that. Now when we define our
Bravais our Bravais lattice class up top. We can also include some
Logic in here. Like for example, we say if
our
self dot crystal
Underscore system is equal to we're going to do two equals because it's a logic
It's comparing it right if that's equal to cubic then we can make something happen. We can say okay in that case
we know that self dot alpha equals 90 that's the angle between two of the
Axes and we know that self dot beta another angle is also equal to 90 and that self dot gamma is
Equal to 90. So what's great about this is imagine if we're gonna be doing Bravais lattice for a bunch of different materials
We're probably gonna come across a bunch of them in our cubic by doing this inside of this initialization, right?
We can sort of define a bunch of things right off the bat every time that something comes in cubic
We can load up a whole bunch of information like this now when we run it because that material is cubic
If we open up copper, it will have additional information, right?
It has stored that the alpha beta and gamma are all 90 degrees
But since we haven't defined it for magnesium yet, it hasn't told us what those things are. Alright, let's show you one more thing
So we've talked about that. We can write a help file for this, which is great
We've shown you the initialization thing, which is pretty slick now
The third thing I want to show you is that we can do custom methods, right?
So let's do a custom method that calculates how many atoms there are per unit cell, right?
So we're gonna call this we're gonna define a function right so def the keyword there and then let's name this function
We'll call it atoms in
the
Unit cell right some you can name whatever that's what we're gonna name it
Okay, and what does it take as an input instead of having to send it the crystal system and the lattice parameter and all that
Jazz, you could just send it self, right? And so what we're gonna do when we go to use this function
It'll look like this. So let's come down here
Well for now, let's just put pass inside of it. We'll show you what we're gonna do in a moment
So now we want to use this we would say like atoms per
Cell would be equal to we're going to run the function atoms
In in
We want the function and we're going to send it the copper instance of the Bravais lattice class
Okay, and inside this copper it's got all the information that it could need to calculate
How many atoms are there per unit cell? So in the function for atoms per unit cell, let's go ahead and explicitly tell it
so we're gonna say if the
if self dot
crystal system
Equals cubic and then we're gonna say and if if self dot centering
type
Equals face, right? So now we know that it's a face centered cubic crystal structure. So now we can say that the
atoms
Per cell is equal to 4 and we can return
atoms
Per cell. Okay, so great. Let's go ahead and run this
Which there's see what our error is here. Oh, we need two equals because we're doing a comparison there. Okay, so great
Let's run this and we've now created this new variable here atoms per cell and sure enough. It's telling us it's four
Okay, so pretty slick
This is much more compact way of organizing and working with data again
Because in just this little tiny variable called see you for copper
You could store all the information you want about copper and it's gonna make it pretty slick to work with it
Okay, so now that I've shown you sort of the nuts and bolts of how classes work in general
Let me show you an example another one associated with Bravais lattices. Okay?
So here we've got an example where we create a class called Bravais lattice
I initialize it with this in it definite this definite self and we give it a name, right?
So now you've got self dot name. So some sort of name of whatever working on now here
I've created a crystal list right? So in this crystal list, you've got aluminum copper and tungsten
Okay, I'm gonna define some functions what I'm gonna call centering
So what it's gonna do is gonna take C
Which will be a member it will be an instance of the Bravais lattice class and the field is centering and it's going to ask
Him says is there simple body base or face centering and the person is going to type in SBC or F for that
Okay, so it's gonna store that string SBC or F into this C dot centering. Okay object and then it's gonna return that
okay, then we have this other function called lattice parameters lattice params where a
C dot a C dot B and C dot C
That's C as a member is an instance of the Bravais lattice class
Then the a B and C are gonna correspond to the a B and C lattice parameters
So it's asking it's saying hey enter the lattice parameters and then it's doing dot split
So they're gonna enter three numbers with spaces between them and it's going to unpack those to these C a C B and C C
Okay, and it's gonna return those same thing with angles
It's gonna ask you to enter the lattice angles and it's going to split them
So if you enter in three numbers with spaces between them, it's going to unpack those into the alpha beta gamma, right?
So we're gonna start out with this crystals
Dictionary, it's an empty dictionary, right? It's this variable. It's a dictionary called crystals right now and then we're gonna say
For crystal in crystal list, so it's doing a for loop
It's going through this crystal list and for each member right for each instance inside of this
It's going to do the following inside of this for loop. So first off it's going to say that C
The object C is equal to a Bravais lattice class of whatever that crystal is. So the first one's gonna be aluminum
It's going to create aluminum where by doing that right off the bat
It's going to be named aluminum. Okay, and
Next thing it's going to do it's going to print the name
So here we're doing print and then the F out front is a fancy way to do
Text where you can print it material and then where whatever you have inside these curly brackets
It'll actually you can just input the variable name there and it will fill it in which is nice
Okay, then it's going to run the three functions that we did above lattice parameters lattice angles and centering
So it's gonna ask the user to input that information what the last parameters are
What's the centering what are the angles and then from that it's gonna go through a series of different logical
questions to figure out what
Bravais lattice this one belongs to you
The first thing it's going to try and figure out is what crystal system it belongs to by examining whether the lattice parameters are equal
to one another and whether the angles are 90 you can figure out if it's cubic if
Two of them are equal to one and if they're all equal to one another but the angle is not 90 then it's trigonal
And so you can go on down. You can figure out what the logic should be for tetragonal hexagonal
Orthorhombic triclinic monoclinic, so I'm gonna skip some of that logic
And then once it's ascertained that it's gonna ask about the centering and based off of the centering
It's now going to spit back out the C dot Bravais
So it's going to store in the C dot Bravais field this information about what type it is
So let's show you this how it works just by running it. Okay, so I'm going to run this program
The first thing it's asking so it says the material is aluminum. It says enter the lattice parameter. So that's three point six one five
three point six one five and three point six one five
Now it's saying enter the angles. We know this is cubic. So that means it's 90 90 and 90
Okay, and then it says is there simple s body B base C or face centering? This is face centered
Okay. Now it's saying to move on to the next one copper
Oh, I think I did the long lap wrong lattice parameters, but whatever for copper. Let's do this one
Let's say it's three three and three, you know, it doesn't matter
The angles are also 90 90 and 90 and the face centering. It's also face centering. Okay now for tungsten
I think tungsten is simple cubic
Anyways, let's punch in. I don't know what the last parameters. Let's just put that it's five five five
The angles are also going to be 90 90 and 90 and then it is simple
Okay, so by running that the last thing it prints it says for each name and crystal lattices. It's printing
crystal
Name, so remember crystals is a dictionary if we come up over here crystals is a dictionary and inside the dictionary is stored
Three objects which are bravae lattice type objects and each one of these stores all the information
They got generated there, right? So it was able to
When we print it we say okay go to crystals now the keyword right because keep dictionaries
Crystals is a dictionary. So when you call it, you have to give it the keyword and it returns a value
So the keyword that we're giving it is the name so that's gonna allow it to tell us whether we're talking about aluminum
tungsten or copper right and then it's returning the
Field bravae is right because now that it's a figured out like oh when we when we open this up copper
We realize that's a bravae lattice. So now we can apply the field
Which we saved as bravae lattice for this is an example of how you can use classes
To store information in really compact and useful ways. Okay on in the next video

