How to include Python Modules in a D Program?

Kirk McDonald kirklin.mcdonald at gmail.com
Mon Sep 24 17:15:54 PDT 2007


Jon wrote:
> It sounds like either way is not that easy. -_- There wouldn't be a 
> python to D convertor anywhere would there? Something like the .h to .d 
> convertor?
> 

Please don't top-post.

No. There's no sensible way to directly use Python code in D code. 
Python is dynamically typed. It is highly introspective. It has 
eval/exec. Things such as integers, functions, and classes are 
first-class objects in Python. Python distinguishes between runtime and 
compile-time much differently than D does. (Much more happens at runtime 
in Python than in D.)

What you /can/ do, and what the PydObject class in Pyd does, is write a 
class wrapping a PyObject* which overloads all of the operators, and 
delegates them off to the appropriate Python/C API functions. A small 
example of PydObject's use can be found in the presentation I gave at 
the D conference:

http://pyd.dsource.org/dconf2007/presentation.html

Just search for "PydObject" on that page. Note that this demonstrates 
the use of PydObject when extending Python with Pyd. Using it when 
embedding Python will look a little different.

-- 
Kirk McDonald
http://kirkmcdonald.blogspot.com
Pyd: Connecting D and Python
http://pyd.dsource.org


More information about the Digitalmars-d-learn mailing list