www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - PyD - accessing D class fields in Python

reply clothlen <clothlen0+dlang gmail.com> writes:
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.
Mar 18
parent Bastiaan Veelo <Bastiaan Veelo.net> writes:
On Monday, 18 March 2019 at 22:25:10 UTC, clothlen wrote:
 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")
Maybe you need `Member!(“b”, “rw”)`?
 	);
 }
If that doesn’t work, maybe you find help by filing an issue on PyD.
Mar 19