在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'}

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...