From: stephan at pcrm.win.tue.nl (Stephan Houben)
Date: 23 Apr 1999 10:48:07 +0200
Subject: Emulating C++ coding style
References: <371F8FB7.92CE674F@pk.highway.ne.jp>
Message-ID: <m3zp3zlo3c.fsf@pcrm.win.tue.nl>
Content-Length: 2093
X-UID: 858                                                  

Thooney Millennier <thooney at pk.highway.ne.jp> writes:

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

Perhaps it would be better to learn how to these things in Python.

> 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__ 

This one is not needed in Python. Python's module mechanism, being 
considerably less brain-dead than C++'s excuse for a module system,
can actually figure out itself whether a particular module has already
been loaded (gasp!).

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

Even in C(++), I would solve this like:

int pyproc(const char *arg)
{
  return printf("%s", arg);
}

Why bother with the #define ?

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

Just do:
print "hello python"

If you want a more OO approach, use:
import sys
sys.stdout.write("hello, python" + "\n")

You can replace sys.stdout with any object that bothers to define a write()
method. 

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

Sorry, no constants in Python.

> 4. access to class's members using "::"
>   e.g. some_class::static_value
>   e.g. some_class::static_func(x)

Well, you can do:

class MyClass:
    static_value = 1
    def static_func(x):
        print x

and then:
MyClass.static_value = 2
MyClass.static_func("hello")

However, you get Strange Results(TM) if you access static_value and
static_func() via an instance of MyClass. 
So I guess you better ignore this idiom.

There are several other suggestions to simulate a "real" static member
in Python. Read Dejanews for more info. However, the best thing to do
is to just use some other idiom. 

For "static methods":
If you really feel they belong to your class, you just create a method
that ignores its "self" parameter. Otherwise, use a global function.

For "static variables":
Make them module-global. Of course, mutations should go via accessor
functions (IMHO), so see "static methods" for more info.

Greetings,

Stephan 





