在Cython中按名称调用cdef函数

时间:2017-09-23 10:40:21

标签: python cython

我在Cython中有一堆cdef函数,由def文件中的pyx函数调用,例如:

cdef inline void myfunc_c(...):
    (...)
    return

def wrapper(...):
    myfunc_c(...)
    return

这很有效。但是为了简化不必为每个cdef函数设置python包装器,我试图通过将cdef函数分配给字典或类似的函数来索引def wrapper(operation): if operation == 'my_func': func = myfunc_c func(...) return 函数:

myfunc_c

但这不起作用。 Cython抱怨说它不知道cpdef的类型。

有没有办法按名称索引或调用locals()['myfunc_c']函数(例如使用字符串)?我也试过像{{1}}这样的东西,但这也不起作用。

1 个答案:

答案 0 :(得分:5)

对于import { Component, Input, OnInit, AfterViewInit, ChangeDetectionStrategy, OnDestroy } from '@angular/core'; @Component({ selector: 'slider-detail', templateUrl: './slider-detail.component.html', styleUrls: ['./slider-detail.component.scss'], changeDetection: ChangeDetectionStrategy.OnPush }) export class SliderDetailComponent implements OnInit, OnDestroy { @Input() items: any; @Input() title: string; @Input() url: string; private _swiper: any; constructor() { } ngOnInit() { } ngAfterViewInit() { // Initialize Swiper this._swiper = new Swiper('.swiper-container', { initialSlide: 0, direction: "horizontal", slidesPerView: "auto", slidesPerGroup: 2, nextButton: '.swiper-button-next', prevButton: '.swiper-button-prev', slidesOffsetBefore: 0, slidesOffsetAfter: 0, observer: true, observeParents: true }); } ngOnDestroy() { // this._swiper.update(true); this._swiper.destroy(); } } 函数,这绝对是不可能的 - 它们只定义了C接口而不是Python接口,所以没有内省可用。

cdef函数定义了Python和C接口。在函数中,它们将在cpdef而不是globals()中提供(因为locals()仅提供在该函数中定义的变量。)

我实际上并不认为你想这样做。我想您只想使用locals()而不是cpdef,因为这会自动生成函数的Python包装器。