Boost logo

Boost :

From: Prabhu Ramachandran (prabhu_at_[hidden])
Date: 2000-11-01 11:55:42


hi,

>>>>> "David" == David Abrahams <abrahams_at_[hidden]> writes:

>>
>> class B : public A and has a method - func1(const int x) const
>>
>> I wrapped this and compiled it into a module called test. And
>> in python I get the following.
>>
>> >>> from test import * >>> dir (A) ['__init__', 'func',
>> 'func1'] # This should not show func1.

    David> Hard to say what might be happening here. Oh, I see it
    David> now. Sorry, something should be done about this. In the
    David> meantime, make sure your declare_base() calls come /after/
    David> all methods have been added to all the classes concerned.

I define A_class and then define B_class. I guess you mean put the
declare_base calls after all the B_class.def's, right? So if I have a
C_class derived from A_class, the line B_class.declare_base (A_class)
comes before any of the C_class stuff? Something like this is ok?

py::ClassWrapper <A> A_class (...)
A_class.def...
...

py::ClassWrapper <B> B_class (...)
B_class.def...
...
B_class.declare_base (A_class);

py::ClassWrapper <C> C_class (...)
C_class.def...

Well, I just tried the above with my test code and it seems to work fine.

>>> from test import *
>>> dir (A)
  ['func']
# This is not perfect. But ok. Where is the __init__, __doc__ etc.?
>>> dir (B)
  ['__doc__', '__init__', '__module__', 'func1']
# It works!
>>> a = A ()
>>> b = B ()
>>> dir (a.__class__)
  ['func']
>>> dir (b.__class__)
  ['__doc__', '__init__', '__module__', 'func1']

# consistent. Works fine. Thanks!

>> >>> dir (B) ['__doc__', '__module__'] # This is not good. :(
    David> No, not good. But at least it's consistent with what I
    David> think is going wrong ;-\

yeah! :)

>> >>> B.__bases__ (<Q22pyt5Class1ZQ22py17ExtensionInstance object
>> at 807d644>,) # Is this Ok? I dont know. Maybe.

    David> Yes, it's OK, but probably I ought to fix the repr() for
    David> classes so it prints something more like <extension_class
    David> test.B at 807d644>

Yes, that would be good.

>> func. >>> b.__class__ <Q22pyt5Class1ZQ22py17ExtensionInstance
>> object at 807d90c> # Correct?

    David> Yes, but not pretty. See my earlier comment. (run it
    David> through c++filt for a nicer-though-not-ideal
    David> representation)

What is c++filt??

>> The reason I am particular about these attributes is because
>> there is some pretty useful stuff that one can do using these.
>> I use this routinely to generate a pipeline browser for VTK.

    David> What's a pipeline browser?

Well, VTK is a high level graphics library that has a pipeline
architecture. Basically, one has source, sink, filter and process
objects. One "connects" these objects to form a visualization network
or a visualization pipeline. I wrote some code with python +Tkinter
that graphically represents the pipeline and allows one to configure
the objects in that pipeline. Look here if you want more information
http://freshmeat.net/projects/vtkpipeline

>> Is it also possible to get the number of arguments that a
>> function expects?? Sorry to pester you with obscure things
>> like this. :)

    David> Probably, though as I wrote earlier it might be a lot of
    David> work. Also, as I wrote earlier I would still like to know
    David> how one does this for a regular Python function. I'd rather
    David> mimic the native interface where possible. Can you show how
    David> it is done?

Here are simple examples that illustrates how python does it.

>>> def t (x):
  ... print "ok", x
  ...
>>> dir (t)
  ['__doc__', '__name__', 'func_code', 'func_defaults', 'func_doc', 'func_globals', 'func_name']
>>> t.func_code
  <code object t at 80813f8, file "<stdin>", line 1>
>>> dir (t.func_code)
  ['co_argcount', 'co_code', 'co_consts', 'co_filename', 'co_firstlineno', 'co_flags', 'co_lnotab', 'co_name', 'co_names', 'co_nlocals', 'co_stacksize', 'co_varnames']
>>> t.func_code.co_argcount
  1
>>> class T:
  ... def t (self, x):
  ... print "T", x
  ...
>>> dir (T.t)
  ['__doc__', '__name__', 'im_class', 'im_func', 'im_self']
>>> T.t.im_func
  <function t at 8081e70>
>>> dir (T.t.im_func)
  ['__doc__', '__name__', 'func_code', 'func_defaults', 'func_doc', 'func_globals', 'func_name']
>>>

I hope that was of some use.

>> How does one set the __doc__ ??

    David> Right now, it's only supported for functions of derived
    David> classes in Python. Do you want to write docstrings in C++?

Yeah, if possible. May be useful for some folks?

>> Could the other special attributes __dict__ etc. be added or
>> are they irrelevant (I dont use them, I do use dir, __bases__
>> and __class__).

    David> __dict__ is already supported.

Yes, after I changed the position of the declare_base it works fine.

I have yet another feature request. I just added a private member (an
int called var) to a class A. Under python is there no way that I can
access this data member? For instance

>>> a = A()
>>> print a.var
  Traceback (innermost last):
   File "<stdin>", line 1, in ?
  AttributeError: var
>>>

Or do data members have to exposed separately? Or is this
impossible to do? I hope I am not getting on your nerves with
feature requests. :)

thanks,
prabhu


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