Re: [Tutor] python vs php5

Magnus Lycka magnus at thinkware.se
Thu Dec 4 14:04:58 EST 2003


Ali wrote:
> i've heard that php5 will include declaration of attributes which are 
> private,
> public and protected... aside from that interfaces which will be 
> implemented by
> a class can also be created...
> 
> in line with OO principles, wouldnt these features be good if implemented 
> in python???

I must admit that I don't have a problem with the way 
Python works with variable access.

The subtle cues of _x and __y is simple, and good enough for me.

With...

class X:
    def __init__(self, x):
        self.__private = x

x = X()

..you can cheat Python and access the private variable
self.__private from the outside as x._X__private, but there
is no way that you will do that by mistake. If you do it
as a quick hack and later fail to notice it, you have very
poor code reviewing practices, and will probably fail to
see lots of semantic and structural problems that are far
more problematic than accessing a variable that you shouldn't.

It's my impression that some languages, such as Java and C++,
are designed to make it difficult for programmers to do "bad
things" on purpose, and at the same times have shortcomings
that repeatedly make programmers try to do these bad things
to work around the stupid language.

Python is rather designed to provide a flexibility that
reduces the need to to "bad things", and let the programmers
take responsibility for their own actions, and instead try
to prevent them from making common mistakes. Thus the block
structure by indenting and syntax error for "if a = 0:", while
peeking at private variables is permitted and simple but made
to stick out in the code.

Interfaces are interesting, but the problem is to design
them in such a way that we keep the flexibility of python.
This is not trivial, and few python programmers would be
satisfied with interfaces which was as restrictive as the
typical interfaces of more static languages such as C++ and
Java.

This has been discussed in the Python community for at least
five years, but obviously noone thought it was important 
enough to go ahead and make an implementation that was so
good that it was accepted. There is a Python Enhancement
Proposal under consideration for this. 

See http://www.python.org/peps/pep-0245.html

-- 
Magnus Lycka, Thinkware AB
Alvans vag 99, SE-907 50 UMEA, SWEDEN
phone: int+46 70 582 80 65, fax: int+46 70 612 80 65
http://www.thinkware.se/  mailto:magnus at thinkware.se



More information about the Tutor mailing list