Introduction to Programming using Python 1st Edition

Published by Pearson
ISBN 10: 0132747189
ISBN 13: 978-0-13274-718-9

Chapter 7 - Objects and Classes - Section 7.5 - Hiding Data Fields - Check Point - MyProgrammingLab - Page 229: 7.16


Making data fields private protects data and makes the class easy to maintain.

Work Step by Step

To prevent direct modifications of data fields, don’t let the client directly access data fields. This is known as data hiding. This can be done by defining private data fields. In Python, the private data fields are defined with two leading underscores. You can also define a private method named with two leading underscores. Private data fields and methods can be accessed within a class, but they cannot be accessed outside the class. To make a data field accessible for the client, provide a get method to return its value. To enable a data field to be modified, provide a set method to set a new value.
Update this answer!

You can help us out by revising, improving and updating this answer.

Update this answer

After you claim an answer you’ll have 24 hours to send in a draft. An editor will review the submission and either publish your submission or provide feedback.