Musings about Python syntax

Robert Meegan rmeegan at murl.com
Tue Oct 19 15:03:44 EDT 1999


On Tue, 19 Oct 1999 steve_allison at my-deja.com wrote:

> 
> i) if Python is as object orientated as is claimed, why are things like
> len() not member functions of list/tuples ?
> 

>From the FAQ (http://www.python.org/doc/FAQ.html):

6.5. Why does Python use methods for some functionality (e.g.
list.index()) but functions for other (e.g. len(list))?

Functions are used for those operations that are generic for a group of
types and which should work even for objects that don't have methods at
all (e.g. numbers, strings, tuples). Also, implementing len(), max(),
min() as a built-in function is actually less code than implementing them
as methods for each type. One can quibble about individual cases but it's
really too late to change such things fundamentally now.

> ii) similarly, why is 'del' operator like, and not a member function of
> those types that support it ?  having an 'append' method, but a 'del'
> operator seems peculiar.
> 

Also from the FAQ:

4.17  My class defines __del__ but it is not called when I delete the
object.

(severely truncated)

The del statement does not necessarily call __del__ -- it simply
decrements the object's reference count, and if this reaches zero
__del__ is called. 

If your data structures contain circular links (e.g. a tree where each
child has a parent pointer and each parent has a list of children) the
reference counts will never go back to zero. You'll have to define an
explicit close() method which removes those pointers. Please don't ever
call __del__ directly -- __del__ should call close() and close() should
make sure that it can be called more than once for the same object. 

> Are these historical artefacts, or is the language designed liek tis
> for a reason ? 

Yes and yes. 

> I have mostly really liked what I've seen of Python so
> far, but niggles like these are really annoying me!

After you spend more time with Python you'll be able to ignore issues like
these, freeing time to be annoyed with issues of garbage collection,
closure, and how many lambdas can dance on the head of a pin.

--- Robert



Robert Meegan

Murl.com - 
Because it's not just your data, it's your life.





More information about the Python-list mailing list