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 the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.