'using namespace' equivalent

Hi,

I’m writing python code to access ROOT code, and I need to use various variables from a C++ header file (such as enum values).

When I load the header files using gROOT->ProcessLine(.include…) I get access to the values I need in python using:
ROOT.firstnamespace.secondnamespace.varname

Unfortunately, this tends to make a huge mess of the code. Is there a way of emulating the behaviour of ‘using namespace…’ to make this more concise?

With regular python code, one would use ‘from module.sub.sub2 import …’. However, using ‘from ROOT.namespace import …’ I get ‘no module named namepsace’.

Any ideas?

Just assign the namespace to a more convenient python label. If you need to use ROOT.namespace1.namespace2.foo() for a lot of different foos, then you can do

bar = ROOT.namespace1.namespace2
bar.foo()

Because namespaces are such an inherent and innocuous part of python, the solution ends up looking pretty simple. = )

Jean-François

Hi,

agree with Jean-François.

That said, in cppyy I had a feature that I wanted to backport for a while and I just did (is in v5-34-00-00-patches and trunk). The problem with it is bootstrapping (I should write a custom finder for import, but haven’t had the time yet to figure out how that would work for older pythons).

Basically, this feature allows:ROOT.namespace1.namespace2 from ROOT.namespace1.namespace2 import *The first line is needed to get all the auto-loading and lazy creation done and over with, as well as to register the namespace as a module. The second line is the using-style. Note that this does not provide the kind of lazy lookup that “from ROOT import *” gives you: it’s a full ref-copy of everything available in the namespace2 at that point in time only.

Cheers,
Wim

Hi,

That’s perfect. I should have realised.

Thanks a lot.