inherit
Inherited in Python
The class with Boost.Python extension supports single inheritance and more inheritance in Python. You can blend the built-in Python class and extended classes in the derived class. As long as the Boost.Python extension class is the base class in Python new class In this, the result is an extension class:
>>> Class MyPythonClass:
... def (): return 'mypythonclass.f ()'
...
>>> IMPORT MY_EXTENSION_MODULE
>>> Class Derived (my_extension_module.myextensionclass, mypythonclass):
... '' 'this is an extension class' ''
... Pass
...
>>> x = derived ()
>>> X.f ()
'Mypythonclass.f ()'
>>> x.g ()
'Myextensionclass.g ()'
Reflection C inheritance
Boost.Python also allows us to provide inheritance relationships between C classes, which can be passed as parameters to those values, pointers, pointers, references, and members of Class_Builder <> Declare_base is used to establish a basis A method of relationship between classes and derived classes.
#include
Struct base {
Virtual ~ base () {}
Virtual const char * name () const {return "base";
}
Struct Derived: Base {
Derived (): x (-1) {}
Virtual const char * name () const {return "derived";
INT X;
}
Std :: auto_ptr
Return std :: auto_ptr
}
Const char * get_name (const base & b) {
Return B.Name ();
}
INT GET_DERIVED_X (Const Derived & D) {
Return D.X;
}
#include
// Namespace Alias for Code Brevity
Namespace python = boost :: python;
BOOST_PYTHON_MODULE_INIT (My_Module)
{
Notes? TRY
Notes? {
笺 笺 笺 Python :: MODULE_BUILDER MY_MODULE ("my_module");
笺 笺 Python :: Class_builder
No. Base_class.def (Python :: Constructor
笺 笺 Python :: Class_builder
笺 笺 笺 Derived_class.def (Python :: Constructor
// establish the inheritance rellationship between base and derivedderived_class.declare_base (base_class);
MY_MODULE.DEF (Derived_as_base, "derived_as_base");
MY_MODULE.DEF (get_name, "get_name");
MY_MODULE.DEF (GET_DERIVED_X, "GET_DERIVED_X");
}
Notes? Catch (...)
Notes? {
笺 笺 Python :: Handle_Exception ();? // deal with the exception for python
}
}
Then in Python:
>>> from my_module import *
>>> Base = base ()
>>> Derived = Derived ()
>>> GET_NAME (BASE)
'Base'
The derived packaging object can be passed to a place where the base class is required.
>>> GET_NAME (Derived)
'Derived'
The derived packaging object can be passed to a place where the derived class type is required, but the type information is lost.
>>> GET_DERIVED_X (Derived_as_base ()))
-1
Inheritance without virtual functions
If you have no virtual functions because of some reasons, you still want to provide the inheritance relationship between base classes and derived classes, use Boost :: Python :: without_downcast as the second parameters of Declare_Base:
Struct base2 {};
Struct derived2 {int f ();
...
Python :: Class_builder
Base2_class.def (Python :: Constructor
Python :: Class_builder
Derived2_class.def (Python :: Constructor
Derived_class.declare_base (base_class, python :: without_download);
This method allows Derived2 objects to be passed to places where the base2 object is required, but will not cause implicit conversions from the Base2 delegation pointers to the DeriveD2 type.
NEXT: Special Method and Operation Support Previous: Function Reserved Up: Top
© David Abrahams 2001 All rights reserved. This document allows copy, use, modification, sale, and distribution, premise this copyright statement must appear on all copies. The provision of this document does not assume any direct or implicit guarantees, and does not make the statement that is suitable for any purpose.
Update Date: November 26, 2000