Boost logo

Boost :

From: David Abrahams (david.abrahams_at_[hidden])
Date: 2001-10-30 01:54:00


Phil,

The instance's __dict__ can be accessed by writing instance.__dict__. I am
guessing that your "attribute_set" is a data member of HDFFile. If you store
attribute values in there, they won't show up in the __dict__. Your data
members don't show up in the __dict__ either (they're C++ objects, not
Python objects of course). Maybe the best way to illustrate is with some
Python code analogous to what you've done:

>>> attribute_sets = {}
>>> class X:
... def __init__(self):
... attribute_sets[self] = {}
... def __setattr__(self, name, val):
... attribute_sets[self][name] = val
...
>>> outfile = X()
>>> outfile.test = "a string"
>>> outfile.__dict__
{}

Why are you storing attributes in your own attribute_set? The instance
already has a __dict__ !

===================================================
  David Abrahams, C++ library designer for hire
 resume: http://users.rcn.com/abrahams/resume.html

        C++ Booster (http://www.boost.org)
          email: david.abrahams_at_[hidden]
===================================================

----- Original Message -----
From: "Phil Austin" <paustin_at_[hidden]>
To: <boost_at_[hidden]>
Cc: "Phil Austin" <paustin_at_[hidden]>
Sent: Monday, October 29, 2001 5:40 PM
Subject: [boost] Boost Python: how to get at instance __dict__?

>
> I'm a little confused about how to access an instance's __dict__
> once I've overridden getattr and setattr using BPL.
> The default getattr and setattr provided by boost
> work as I expect, i.e. after creating an instance of the wrapped
> class called outfile, the lines:
>
> > outfile.test="a string"
> > print "attribute test: ",outfile.test
> > print "asking for dict: ",outfile.__dict__
>
> produce:
>
> attribute test: a string
> asking for dict: {'test': 'a string'}
>
> However when I add my own versions of __getattr__ and __setattr__
> in BOOST_PYTHON_MODULE_INIT:
>
> HDFFile_class.def(&HDFFile::get_attribute, "__getattr__");
> HDFFile_class.def(&HDFFile::set_attribute, "__setattr__");
>
> Where:
>
> py::ref HDFFile::get_attribute(const py::string& pyname)
> {
> std::cout << "in get_attribute " << pyname.c_str() << std::endl;
> if (std::strcmp(pyname.c_str(), "__dict__") == 0){
> std::cout << "caught dict " << std::endl;
> }
> return attribute_set->get_attribute(pyname);
> }
>
> void HDFFile::set_attribute(const py::string& pyname, const py::ref& x)
> {
> std::cout << "in set_attribute " << pyname.c_str() << std::endl;
> attribute_set->set_attribute(pyname, x);
> }
>
> I can't trap attempts to read __dict__ from Python, or alternatively,
> figure out how to get my attributes into the instance namespace.
>
> With the new getattr and setattr,
>
>
> > outfile.test="a string"
> > print "attribute test: ",outfile.test
> > print "asking for dict: ",outfile.__dict__
>
> now produces:
>
> in set_attribute test
> attribute test: in get_attribute test
> a string
> asking for dict: {}
>
> Any suggestions appreciated, I'm happy to provide the module
> code if it will help.
>
> Regards, Phil Austin
>
>
> Info: http://www.boost.org Unsubscribe:
<mailto:boost-unsubscribe_at_[hidden]>
>
> Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/
>
>
>


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk