O'Reilly logo

Introduction to Programming in Python: An Interdisciplinary Approach by Robert Sedgewick, Kevin Wayne, Robert Dondero

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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, ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required