Boost logo

Boost Users :

Subject: Re: [Boost-users] Boost::python readonly access to nonconstant class members
From: Dave Abrahams (dave_at_[hidden])
Date: 2010-12-21 14:14:43


At Tue, 21 Dec 2010 15:47:38 +0100,
Ralf Goertz wrote:
>
> Hi,
>
> is it possible to have python have readonly access to class members
> that are non constant?
>
> typedef map<string,string> Links;
>
> class DataSet {
> Links links;
> }
>
> BOOST_PYTHON_MODULE(_boosttest) {
> class_<Links>("Links")
> .def(map_indexing_suite<Links>());
>
> class_<DataSet>("DataSet")
> .def_readonly("links",&DataSet::links);
> }
>
> Although DataSet.links is defined readonly I can change it from within
> python.

If you mean that you can do

   d = DataSet()
   d.links = 1

that would be a Boost.Python bug.

If instead you mean that you can do

   d = DataSet()
   d.links["foo"] = "bar"

that would be unsurprising (to me), intended behavior; that's just how
the Python object model works.

> What can I do to prevent python from overwriting links?

You could always do the equivalent of

  del DataSet.__dict__["__setitem__"]

to disable it.

-- 
Dave Abrahams
BoostPro Computing
http://www.boostpro.com

Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net