Emulating C++ coding style

David Cuthbert dacut at kanga.org
Fri Apr 23 20:21:53 EDT 1999


William Tanksley <wtanksle at dolphin.openprojects.net> wrote:
> On Fri, 23 Apr 1999 06:08:07 +0900, Thooney Millennier wrote:
> >2. stream class
> >  e.g. cout << "hello python."<<endl;
>
> VERY bad idea.  Bad even in C++.

Out of curiosity, why is this a "VERY bad idea"?  Personally, I really like
the ideas behind the streams interface in C++ (though the implementations
are usually somewhat lacking) and use Python file objects in the same
manner.  Am I doing something wrong?

For that matter, I don't see how C++ streams are really any different than
Python's file objects.  To summarise:

In C++:    Base interface definition in {i,o}stream and streambuf.
In Python: Base interface definition in file objects.

In C++:    New streams are defined by inheriting from {i,o}stream and
           streambuf, and by providing an implementation for the relevant
           methods.
In Python: New streams are defined by providing methods with the same
           name and arguments as those for files.

In C++:    User-defined classes are streamed in/out by providing:
               ostream& operator << (ostream&, UserClass const&)
               istream& operator >> (istream&, UserClass&)
           methods/functions (or by using a persistence library).
In Python: Classes are streamed in/out by providing a __str__() method
           or by using one of the (nicely built-in :-) persistence
           modules.

The biggest difference that I can see is that C++ methods are static (i.e.,
checked and linked at compile time) vs. dynamic in Python (which is how you
get away without inheriting from a file base class).  But this isn't
specific to streams.







More information about the Python-list mailing list