CHAPTER 12
Working with Classes
In this chapter, you work with Python’s classes, which enable you to create custom objects in your scripts. You learn to create a class, create objects based on that class, and work with those objects. Because of the nature of classes, this chapter is set up as an extended example using Visual Studio Code rather than terminal windows, and we recommend you work through the chapter from start to end.

Understanding Classes and Instances
Create a Class and Instantiate Instances
Understanding Class and Instance Attributes
Set Class and Instance Attributes
Understanding Classes and Instances
In Python, a class is a template for creating objects of a particular type — a “class” of object, in computer terms. When you need to create standardized objects of the same type, you can declare a class for that type of object. You can then create what are called instances of the class — individual objects based on the class.
In this chapter, you create a class called BranchOffice to use for creating objects that store data on the individual branch offices of a notional company. After creating the class, you can create a separate instance for each branch office.
When Should You Create a Class?
Consider creating a class when you ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access