From: faassen at pop.vet.uu.nl (Martijn Faassen)
Date: Fri, 23 Apr 1999 00:05:00 +0200
Subject: Emulating C++ coding style
References: <371F8FB7.92CE674F@pk.highway.ne.jp>
Message-ID: <371F9D0C.4F1205BB@pop.vet.uu.nl>
Content-Length: 1924
X-UID: 299                                                  

Thooney Millennier wrote:
> 
> Hello Everyone!
> 
> I usually use C++ ,so I want to make programs like
> I do using C++.

Well, first of all, Python is not C++. Some C++ practices don't, and
shouldn't, translate to Python, and vice versa. Python coding style is
different than C++ coding style, just like Java is different yet again,
etc, etc.

> I don't figure out how to implement the followings
> by Python.
> If you know any solutions,Please Help!
> 
> 1. #define statements
>   e.g. #define __PYTHON_INCLUDED__
Why would you need this? You can use a dictionary and store this kind of
variable in there.

>        #define PYPROC(ARG) printf("%s",ARG)

def pyproc(arg):
    print "%s" % arg

Python doesn't have a preprocessor.

> 2. stream class
>   e.g. cout << "hello python."<<endl;

print "Hello python"

You can override the __str__ method of your own classes determine what
the output for print is for that class.

> 3. const variables
>   e.g. const int NOCHAGE=1;

Though trickery is possible, Python does not strictly have any constant
variables. What you can do is use (global) variables with an preceding
underscore. When importing the module these variables are not
accessible. _like_this.
 
> 4. access to class's members using "::"
>   e.g. some_class::static_value
>   e.g. some_class::static_func(x)

Python does not support static methods (or 'class methods'). Usually a
module level global function suffices for this purpose.

A static value can be created like this (besides using a global variable
in a module):

class Foo:
   self.shared = 1

   def __init__(self):
       print self.shared


Other people will likely come up or refer to the more complicated ways
to emulate these things in Python, but they're slower and nobody
actually seems to use them in practice.

Python is more dynamic than C++, and that is reflected in the way the
language is used. I hope this helps.

Regards,

Martijn




