Как я могу получить новый экземпляр из класса Python с помощью go-python? - PullRequest
1 голос
/ 29 мая 2019

Я пытался получить новый экземпляр из класса Python Test. Тем не менее, он возвращает ноль, когда я вызвал функцию PyInstance_New (). Вот мой тестовый код.

// hello.py
class Test(object):
    def __init__(self, name):
        print(name)
    def hello(self, line):
        return line

// test.go
import (
    "github.com/sbinet/go-python"
    "log"
)

func init() {
    err := python.Initialize()
    if err != nil {
        panic(err.Error())
    }
}

func main() {
    hello := python.PyImport_ImportModule("hello")
    if hello == nil {
        log.Fatalf("could not import 'hello'\n")
    }

    testGetter := hello.GetAttrString("Test")
    if testGetter == nil {
        log.Fatalf("could not retrieve 'hello.Test'")
    }

    gostr := "here is a test"
    pystr := python.PyString_FromString(gostr)
    test := python.PyInstance_New(testGetter, pystr, nil)
    if test == nil {
                 // log fail at here
        log.Fatalf("could not retrieve 'test'")
    }
}

Неправильно ли я назвал функцию? Как я могу получить новый экземпляр из Python? Спасибо.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...