[Tutor] Tutor FAQ

Mike Hansen mahansen at adelphia.net
Thu May 4 03:16:18 CEST 2006


Well, I posted a few questions to http://pyfaq.infogami.com/tutor-index 
late last week. Since the next questions and answers are a bit on the 
long side, I'll send them to this list in multiple messages. Please let 
me know if you have any corrections or clarifications. Below is a 
reworked question from last week.

---------------------------------------------------------------------
How do I make public and private attributes and methods in my classes?

Python followws the philosophy of "we're all adults here" with respect 
to hiding attributes and methods. i.e. trust the other programmers who 
will use your classes. You should use plain attributes whenever 
possible.

You might be tempted to use getters and setters, but the only reason to 
use getters and setters is so you can change the implementation later 
if you need to.  However, Python
allows you to do this with properties

http://www.python.org/download/releases/2.2.3/descrintro/#property

If you really must hide an attribute or method, Python does do name 
mangling. By putting two underscores before the attribute or method 
name and not putting two underscores after the name, Python will mangle 
the name by putting the class name in front of it.

For an short example see
http://en.wikipedia.org/wiki/Name_mangling#Name_mangling_in_Python

[Paraphrased from a post to comp.lang.python by Steven Bethard]



More information about the Tutor mailing list