Q&A

Q. Can I define a class in a file whose name is unrelated to the class name? Can I define more than one class in a single .py file?

A. Yes and yes, but we do not do so in this chapter as a matter of style. In CHAPTER 4, we will encounter a few situations where these features are appropriate.

Q. If __init__() is technically not a constructor, what is?

A. Another special function, __new__(). To create a object, Python first calls __new__() and then __init__(). For the programs in this book, the default implementation of __new__() serves our purposes, so we do not discuss it.

Q. Must every class have a constructor?

A. Yes, but if you do not define a constructor, Python provides a default (no-argument) constructor automatically. With our conventions, ...

Get Introduction to Programming in Python: An Interdisciplinary Approach now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.