library: libPyROOT
#include "TPython.h"

TPython


class description - header file - source file
viewCVS header - viewCVS source

class TPython

Inheritance Inherited Members Includes Libraries
Class Charts

Function Members (Methods)

Display options:
Show inherited
Show non-public
public:
TPython()
TPython(const TPython&)
virtual~TPython()
static Bool_tBind(TObject* object, const char* label)
static TClass*Class()
static const TPyReturnEval(const char* expr)
static voidExec(const char* cmd)
static voidExecScript(const char* name, int argc = 0, const char** argv = "0")
virtual TClass*IsA() const
static voidLoadMacro(const char* name)
TPython&operator=(const TPython&)
static voidPrompt()
virtual voidShowMembers(TMemberInspector& insp, char* parent)
virtual voidStreamer(TBuffer& b)
voidStreamerNVirtual(TBuffer& b)
private:
static Bool_tInitialize()

Class Description

                          Python interpreter access
                          =========================

 The TPython class allows for access to python objects from CINT. The current
 functionality is only basic: ROOT objects and builtin types can freely cross
 the boundary between the two interpreters, python objects can be instantiated
 and their methods can be called. All other cross-coding is based on strings
 that are run on the python interpreter.

 Examples:

  $ cat MyPyClass.py
  print 'creating class MyPyClass ... '

  class MyPyClass:
     def __init__( self ):
        print 'in MyPyClass.__init__'

     def gime( self, what ):
        return what

  $ root -l
  // Execute a string of python code.
  root [0] TPython::Exec( "print \'Hello World!\'" );
  Hello World!

  // Create a TBrowser on the python side, and transfer it back and forth.
  // Note the required explicit (void*) cast!
  root [1] TBrowser* b = (void*)TPython::Eval( "ROOT.TBrowser()" );
  root [2] TPython::Bind( b, "b" );
  root [3] b == (void*) TPython::Eval( "b" )
  (int)1

  // Builtin variables can cross-over by using implicit casts.
  root [4] int i = TPython::Eval( "1 + 1" );
  root [5] i
  (int)2

  // Load a python module with a class definition, and use it.
  root [6] TPython::LoadMacro( "MyPyClass.py" );
  creating class MyPyClass ...
  root [7] MyPyClass m;
  in MyPyClass.__init__
  root [8] char* s = m.gime( "aap" );
  root [9] s
  (char* 0x41ee7754)"aap"

 It is possible to switch between interpreters by calling "TPython::Prompt()"
 on the CINT side, while returning with ^D (EOF). State is preserved between
 successive switches.
Bool_t Initialize()
 Private initialization method: setup the python interpreter and load the
 ROOT module.
void LoadMacro( const char* name )
 Execute the give python script as if it were a macro (effectively an
 execfile in __main__), and create CINT equivalents for any newly available
 python classes.
void ExecScript( const char* name, int argc, const char** argv )
 Execute a python stand-alone script, with argv CLI arguments.

 example of use:
    const char* argv[] = { "1", "2", "3" };
    TPython::ExecScript( "test.py", sizeof(argv)/sizeof(argv[0]), argv );
void Exec( const char* cmd )
 Execute a python statement (e.g. "import ROOT").
const TPyReturn Eval( const char* expr )
 Evaluate a python expression (e.g. "ROOT.TBrowser()").

 Caution: do not hold on to the return value: either store it in a builtin
 type (implicit casting will work), or in a pointer to a ROOT object (explicit
 casting to a void* is required).
Bool_t Bind( TObject* object, const char* label )
 Bind a ROOT object with, at the python side, the name "label".
void Prompt()
 Enter an interactive python session (exit with ^D). State is preserved
 between successive calls.
virtual ~TPython()
{ }

Author: Wim Lavrijsen, Apr 2004
Last update: root/pyroot:$Name: $:$Id: TPython.cxx,v 1.17 2006/09/28 19:59:12 brun Exp $


ROOT page - Class index - Class Hierarchy - Top of the page

This page has been automatically generated. If you have any comments or suggestions about the page layout send a mail to ROOT support, or contact the developers with any questions or problems regarding ROOT.