Python With Spyder 10: Objects Part 3 – Private Data and Encapsulation

This is the 10th in a series of videos providing a tutorial on Python 2.7 using Anaconda Python and the Spyder IDE. Click here to go to a “home page” for the video series.

This video is the third of four videos introducing objects in Python. This video discusses private data and encapsulation.

The source code used in this video can be found here. You can right click on the link and use “Save As” to save the file.

Note: The source code files are plain text files with a “.txt” extention. You will probably want to change the extensions to “.py” after you download them. If you do so, please be aware that if you have Python installed, the file will become executable, so that it will run if you click on it (accidentally or otherwise).

The video is about 20 minutes long.


Next Video: Inheritance

Video Index: Private Data and Encapsulation

Click on the topics below to jump to that location in the video.

Time Topic
00:41Review of the example
01:22Phone number example of private data
02:10Basic idea of encapsulation
02:42Begin phone number method example
02:58Phone number formats
03:42Private attribute __PhoneNumber
04:53SetPhone() and GetPhone() methods
05:42All methods get self as first argument
07:14“if” statement to test if the phone number is a character string
08:18Use of the “or” keyword in the example
09:37Use of the “in” keyword in the example
10:16Use of the “and” keyword in the example
11:05The line continuation character (“\”)
11:23Use of the “not” keyword in the example
11:45The return statement in the SetPhone() method
11:59Set or create the private attribute __PhoneNumber
13:46Using a “cell” in the editor and cntrl-enter to execute a block of code
14:33Create two instances
14:57Private data cannot be directly accessed
15:15Use the SetPhone() method to set the phone number
16:03Private attribute __PhoneNumber still cannot be directly accessed
16:17Read the phone number with the GetPhone() method
16:45Test the SetPhone() methods with incorrectly formatted phone numbers
18:04Summary of private data in Pythin objects
18:53Summary of Encapsulation
19:12Private methods (functions)

Next Video: Inheritance