is Python fully object oriented ?

Martijn Faassen m.faassen at vet.uu.nl
Sat Jan 13 08:08:52 EST 2001


Ben Hutchison <benh at intelligenesis.net> wrote:
> Crutoy wrote:

>> Hi , i haven't used Python but i do have experience in other languages vb ,
>> java , c++ . Is Python a fully object oriented ? Thanks

> I have just been considering the same questions myself. I do have a few
> concerns with Pythons OO credentials, relative to Java/C++.

> 1. Most significantly, Python requires the "self" or "this" reference to be
> explicitly declared. This seems like a bit of a hassle if you do pure OO
> development with python; lots of typing! Suggests perhaps that OO features were
> built onto initially procedural language.

Heh, like C++? Anyway, Python used objects from the start, but the 'class'
construct was added somewhat later (though still very early on). It's
most an elegant bit of syntactic sugar put on top of a dictionary.

In C++, it's often recommended you do something along these lines:

class Foo {
   int d_bar;

   public:
      Foo(int bar);
};

Foo::Foo(int bar) {
  d_bar = bar;
}

i.e. prefix members with something like 'd_' so that they can be easily
distinguished from local variables and method arguments. This also 
makes writing constructors cleaner, as demonstrated.

In Python, all this takes rather less characters to type, you don't need
coding conventions like d_, and the class is instantly generic to boot:

class Foo:
   def __init__(self, bar):
       self.bar = bar

So, it's not a hassle; it's even less typing, and it's instantly clear
what's going on too.

[snip]
> 2. Access control is more informal in Python than C++/Java. I know in the case
> of Java at least that security provisions depend in part on access control
> being strictly enforced, and additonally there are a number of design
> principles that assume 3 tiered (public, protected, private) access levels.
> * Is Pythons leading underscore mechanism more of a convention?

Yes.

> * It is strong enough to build security mechanisms on?

Do you often need to secure your classes and objects? Anyway, yes, you
can build security mechanisms. It's a dynamic language, after all.

But what is it you really want? Security or encapsulation?

> * Can a class declare a priviledged API availalbe only to subclasses?

Probably, but I've never felt much need to find out. You can just use
the underscore convention; anything without it one is public, everything
with one is private (but accessible by all subclasses), but this is
by convention only. Two underscores and you get some name mangling to boot,
but I've usually found that more of a hassle than it's worth.

Anyway, Python's style of programming is quite different from what you
may be used to in C++ or Java. You shouldn't try to write C++ and Java
in Python. It takes some getting used to, but you may end up liking it
a lot.

Regards,

Martijn
-- 
History of the 20th Century: WW1, WW2, WW3?
No, WWW -- Could we be going in the right direction?



More information about the Python-list mailing list