Python design philosophy

Donn Cave donn at u.washington.edu
Wed Jun 28 17:30:25 EDT 2000


Quoth Eric Lee Green <eric at estinc.com>:
| Steve Juranich wrote:
|> I am *brand* new to python (as of Sunday), and I just got to the section
|> in the tutorial about classes.  I was wondering why there really isn't
|> such an idea as a "private" member of classes? 
|
| Mostly because Python doesn't "really" have classes. What Python "really" has
| are hash tables whose entries are objects (either methods or other data
| objects), and some syntactic sugar for instantiating, calling methods within,
| and zapping these guys. Thus a class instance 'foo' with methods 'bar' and
| 'grr' and data values 'ugh' and 'umm' would actually be a hash table with four
| items in it, { 'bar': <some method> , 'grr': <some method>, 'ugh': 0, 'foo':
| 555 } and then 'foo.bar(x)' is syntactic sugar for foo['bar'](foo,x) . Though
| the interpreter of course will not accept the latter syntax (it enforces the
| class syntax). 
|
| This is one reason why inheritance is so efficient in Python, BTW. All
| inheritence does is add into the hash table for the overall class object those
| entries that were in the base class object. Then when you create a new
| instance, you actually are just creating a copy of the overall class object,
| without having to refer back to the base object(s). 

Hm, this doesn't really work for me.  Are you pulling my leg?

Does inheritance add to the hash table?  No, your derived class does
indeed refer to each of its parents' classes separately at run time,
you can even add functions at run time to a parent class and use them
from an old instance.

Does Python really have classes?  Yes!  Though we can explain some of
their implementation in terms of other Python features, the class is
absolutely integral to the language at a design level.  Though as with
any software there are accidents of implementation, there was also
an element of conscious language design.  Could a language otherwise
like Python be implemented with private functions like C++ (also, see
below!)?  Yes, and I think the price you would pay would be less in
the efficiency of the program than in the efficiency of programming.
It would slow you down as you try to write software, for little
practical benefit.

But in response to popular demand, there actually is a private class
member feature.  Define a member function with two leading underscores,
like "__private_function".  I have worked with code that does this
extensively, and it's kind of a pain in the neck.  It's a rare
programmer who will think to use it, and then really think before
using it.

	Donn Cave, donn at u.washington.edu



More information about the Python-list mailing list