PyD - accessing D class fields in Python

clothlen clothlen0+dlang at gmail.com
Mon Mar 18 22:25:10 UTC 2019


Howdy;

I'm trying to extend my Python program with D, but I'm having 
trouble accessing a D class's field/attribute/property/something.

My D file looks like this:

```
module simpletest;
import pyd.pyd;
import std.stdio;

class ExampleTest{
	int _a = 3;
	int a(){
		return _a;
	}
	void a(int a){
		this._a = a;
	}
	int b = 4;
}

extern(C) void PydMain() {
	module_init();
	wrap_class!(
		ExampleTest,
		Property!(ExampleTest.a),
		Property!(ExampleTest.b)
		// Member!("b")
	);
}

```

and my Python file looks like this:

```
#!/usr/bin/env python3.7
# -*- coding: utf-8 -*-
"""Run with `pipenv run pytest -s tests/test_simple_test.py`."""
import simpletest


def test_class():
	test = simpletest.ExampleTest()
	assert None is print(test.a)
	assert test.a == 3
	test.a = 2
	assert test.a == 2
	assert None is print(test.b)
```

The first field, `a`, I can read, but `attribute 'b' of 
'simpletest.ExampleTest' objects is not readable`. `a` has a 
getter and setter function; do I have to make getters and setters 
for all fields I want to share with Python? That sounds like a 
lot of wasted space if so. The only examples at the Github 
(https://github.com/ariovistus/pyd/tree/master/examples) only 
seem to show it with getters and setters.


More information about the Digitalmars-d-learn mailing list