在Cython中返回字典的函数的说明

问题描述

在Cython中,我可以声明一个返回整数函数

cdef int fx(int myInt):
    return myInt

here中记录的所有数据类型相同。

返回整数数组的函数为:

cdef int[:] fx():
   ...
  return myIntArray

是否可以声明一个返回字典函数?怎么样?

谢谢!

disclamer:我是Cython的新手。请评论或编辑我的错误

解决方法

至少对于从python调用函数而言,是 dict

例如 Dict.pyx

# function can be called in cython
cdef dict print_dict():                                                         
    cdef dict a                                                                 
    a = {'a':'areli','b':'asdasd'}                                             
    return a                                                                    
                                                                                

# function to be called in python                                                                            
def test_dict():                                                                
    return print_dict()   

在python中:

import Dict                                                                                                                                                                         
Dict.test_dict()                                                                                                                                                                

给予:

{'a': 'areli','b': 'asdasd'}