|
Boost : |
From: jjphatt_at_[hidden]
Date: 2001-05-02 12:21:07
Howdy folks,
Has anyone thought about supporting the use of C++ get() and set()
methods to define Python attributes? Some other Python/C++ tools
allow you to do this, and it's really helpful.
For example:
A C++ class representing an ideal gas in a reservoir:
class Gas
{
public:
...
// Get the temperature of the gas.
double getTemperature() const;
// Set the temperature of the gas.
void setTemperature(double newTemperature);
// Get the pressure of the gas.
double getPressure() const;
// Set the pressure of the gas.
void setPressure(double newPressure);
// Get the volume in which the gas is contained.
double getVolume() const;
// Set the size of the reservoir containing the gas.
void setVolume(double newVolume);
};
may correspond to a Python class with attributes temperature,
pressure, and volume. There's no need, per se, for us to impose the
C++ notion of get()ting and set()ting attributes if we can just make
the get() and set() functions transparent within Boost.Python.
For example, here's the way we currently have to do things:
>>> from Gas import *
>>> gas = Gas()
>>> gas.setVolume(20.0);
>>> gas.setTemperature(298.0);
>>> gas.setPressure(1.0);
>>> gas.getTemperature()
298.0
It seems to me we could easily add functionality to Boost.Python that
would make it possible to do this:
>>> from Gas import *
>>> gas = Gas()
>>> gas.volume = 20.0 # Calls C++ setVolume(20.0)
>>> gas.temperature = 298.0 # Calls C++ setTemperature(298.0)
>>> gas.pressure = 1.0 # Calls C++ setPressure(1.0)
>>> gas.temperature # Calls C++ getTemperature()
298.0
I work on a scientific code that uses python as a front-end to C++,
and the package we currently use to map C++ classes into Python
supports this functionality, but we would really like to start using
Boost. If I can provide a way for Boost to do this, is there any
interest in this sort of thing?
(BTW, this can be extended to support "read-only" Python attributes,
which have been discussed here before. To make a read-only Python
attribute, you would simply associate the attribute with a given C++
get() function and skip the set() function.)
Thanks,
Jeffrey Johnson
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk