cdef类可以存储未声明(类型)的变量吗?

编程入门 行业动态 更新时间:2024-10-09 06:27:23
本文介绍了cdef类可以存储未声明(类型)的变量吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我很好奇以下内容是否有效,其中只有一些变量在类型声明的类中进行了类型声明.也就是说,在这种情况下,在类之前的cdef是否无效?

I’m curious if the following is valid, where only some of the variables are type-declared in a type-declared class. That is, would cdef before the class be invalid in this case?

cdef class CythonClass: cdef int var1, var2 def __init__(self, a, b): self.var1 = a self.var2 = b self.defaultdict = DefaultDict(DefaultDict([]))

推荐答案

简短答案:

否,您需要声明它.否则,您会收到AttributeError: 'xxx.CythonClass' object has no attribute 'defaultdict'错误.

您始终可以将其声明为(python)对象:

You can always declare it as (python) object:

cdef class CythonClass(object): cdef int var1, var2 cdef object defaultdict # declared as python object

这不是很有效,但是可以.

This won't be very efficient, but it works.

更多推荐

cdef类可以存储未声明(类型)的变量吗?

本文发布于:2023-11-25 17:59:17,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1630758.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:变量   声明   类型   cdef

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!