Reader small image

You're reading from  Learn Python by Building Data Science Applications

Product typeBook
Published inAug 2019
Reading LevelIntermediate
PublisherPackt
ISBN-139781789535365
Edition1st Edition
Languages
Tools
Right arrow
Authors (2):
Philipp Kats
Philipp Kats
author image
Philipp Kats

Philipp Kats is a researcher at the Urban Complexity Lab, NYU CUSP, a research fellow at Kazan Federal University, and a data scientist at StreetEasy, with many years of experience in software development. His interests include data analysis, urban studies, data journalism, and visualization. Having a bachelor's degree in architectural design and a having followed the rocky path (at first) of being a self-taught developer, Philipp knows the pain points of learning programming and is eager to share his experience.
Read more about Philipp Kats

David Katz
David Katz
author image
David Katz

David Katz is a researcher and holds a Ph.D. in mathematics. As a mathematician at heart, he sees code as a tool to express his questions. David believes that code literacy is essential as it applies to most disciplines and professions. David is passionate about sharing his knowledge and has 6 years of experience teaching college and high school students.
Read more about David Katz

View More author details
Right arrow

Simulation with Classes and Inheritance

In the previous chapters, we described the entities we derived from libraries as objects with no further explanation. For example, an entity describing a web page in our previous chapter was an object with its own values and methods—bound functions that seem to have access to objects internals.

In fact, we can create our own structures by defining classes. In this chapter, we'll do exactly that—learn how to define and make the most of classes and their properties. Along the way, we will use classes to create a rather complex system and monitor its behavior—something that classes are very good for. Because of that, classes are used extensively in games, computer graphics...in fact, any type of task where creating specific entities seems useful.

In this chapter, we will cover the following topics:

  • Writing a class...

Technical requirements

Understanding classes

You can think of classes as blueprints for their instances—for example, a Car class will describe the generic properties of a car, while a specific instance of this class will describe a particular car with its own characteristics. As such, classes provide a way to store information and related functions linked to the instances. In our example, the Car class can store a drive function in its body, which will rely on the gasoline value for each specific instance. This approach is extremely useful when we describe systems of entities, whether a representation of a physical object (Car), personal information (Contact), or some abstract entity (web page representation).

Let's start with the syntax. Take a look at this example:

class Person:
'''person entity'''
greeting = 'Hi {0}. My name is {1}!'
def...

Using classes in simulation

Let's use this new knowledge for a fun project—modeling evolution. We are going to create a closed, simplistic representation of an ecosystem, containing animals that can age, breed, and survive (or not!) in harsh conditions. To test different scenarios, we will produce a few of those ecosystems, islands, each having different environmental conditions. We'll run the experiment by simulating the behavior of those systems and gather statistics on their dynamics over time. In order to do so, we will build a dedicated class for each of those entities.

Writing the base classes

Before we do any simulation, we should start by writing the basic classes we'll be using, describing the...

Summary

In this chapter, we have discussed the concept of classes and how we can use them to model entities with their properties and methods. Using classes, we created plenty of dynamic systems, illustrated the rules of natural selection, and explored their behavior. Finally, we discussed how to generate and collect a system's telemetry data and plot timelines in order to understand how systems behave.

In the next chapter, we'll review a few important technologies that are beyond Python, but that are extremely useful for productive programming.

Questions

  1. What are classes? When should we use them?
  2. Can we compare two instances of a class, or use arithmetic operations with them?
  3. When should we use inheritance?
  4. What is the use case for data classes?

Further reading

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Learn Python by Building Data Science Applications
Published in: Aug 2019Publisher: PacktISBN-13: 9781789535365
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Authors (2)

author image
Philipp Kats

Philipp Kats is a researcher at the Urban Complexity Lab, NYU CUSP, a research fellow at Kazan Federal University, and a data scientist at StreetEasy, with many years of experience in software development. His interests include data analysis, urban studies, data journalism, and visualization. Having a bachelor's degree in architectural design and a having followed the rocky path (at first) of being a self-taught developer, Philipp knows the pain points of learning programming and is eager to share his experience.
Read more about Philipp Kats

author image
David Katz

David Katz is a researcher and holds a Ph.D. in mathematics. As a mathematician at heart, he sees code as a tool to express his questions. David believes that code literacy is essential as it applies to most disciplines and professions. David is passionate about sharing his knowledge and has 6 years of experience teaching college and high school students.
Read more about David Katz