Welcome back. So, suppose that we have
some data and we want to use classes to represent that data.
So, that data might come from a file,
it might come from an internet source
or in the case that I'm going to go through right now,
it might come from something that we just hand coded.
So in this case, I have a list of city names populations and state.
So, I have city named Detroit with population
680,250 in state Michigan and I have an Ann Arbor population 117,070,
state Michigan et cetera.
So, Pittsburgh this is the population.
State is PA, Mars,
small population in Pennsylvania,
New York large population in New York.
So, let's suppose that I have this data
and let's suppose that I also zip these data together.
So, zip is going to take these separate lists and create a list of tuples.
So, if I print out what the value of city tuples.
I'm going to see that I have a list of tuples where
every city name is coupled with its population which is coupled with the state.
So, suppose that I want to use classes to represent this data.
The person that I would ask is,
what data do I want to be able to put into every instance?
So what should every instance have?
So, what should instances store?
So, in this case let's suppose that I care about all three of these things.
So let's suppose that I care about the city name,
the population and the state.
So I want every instance to store the city name, population and state.
Then I would ask, what do I want every class to you represent?
In this case it's a little bit obvious every instance should represent
an individual city and every individual city it's going to have a name,
population and state, and I'm going to call my class city.
So, let's define our city class.
I'll do that by saying class,
capital C City and in the constructor,
I'm going to accept a name,
population, state and I'm going to set instance variables.
So self.name equals M, Self.population equals P,
self.state equals S. Let's suppose that I want to have a str method,
so def underscore str,
and I want it to return the city name,
state and then population.
So, I'll do this.format, self.name, self.state, self.pop.
Okay, so now that I have this list of
city tuples and I have a class to represent every individual city,
I want to take this list of city tuples and create a list of cities.
So, let's suppose that I want to have a list of cities.
So, I'll say cities.
I'm going to start it out as an empty list and then I'm going to loop over every tuple.
So, I'll say for city tuple in city tuples.
For now I'll just print out what's the value of
city tuple and I can see that I get every tuple on a separate line.
If I wanted to, I could set name,
population, state equal to city tuple.
If by print these out name, population and state.
I'll see I've the city name,
population and state in different variables.
If I want to create a new city with that name population and state.
I'll say city equals a new city with name population and state,
and now City is an instance of the city class.
So if I print out what the value of city.
Oops!
I also need to do spell out population in my str method.
Okay, so now, I'll see that I am
actually calling this def underscore str method on my list of cities.
If I wanted to add that city to my list of cities,
I could say cities.append city and by the end of this for loop cities,
it's going to be a list of city instances so I can say print cities.
You can see I have a list of city instances.
So, some shorter ways of doing this,
one thing that I could do is I could use a list comprehension.
So, rather than having all of these lines,
I could use a list comprehension to say that cities equals a new city with name,
population and state for every name,
population, and state in city tuples.
Now if I do this and actually print out cities,
then I'll see that I get a list of city instances as well.
One really short way of doing this is actually if I do T. I can
actually add this star T. What star does is
it takes this tuple and expands it into a list of arguments.
So, this constructs the same thing.
I'm going to prefer however to instead
explicitly get out the name, population, and state.
That's all for now until next time.