如何用Micropython编写一个可以用参数初始化的类?

2024-04-27 02:55:06 发布

您现在位置:Python中文网/ 问答频道 /正文

我正试图用Micropython编写一个类,它需要用一个参数初始化,但是我很难弄清楚怎么做。你知道吗

我在micropython1.9.4中使用XBee模块

我是这样声明这个类的:

class myClass:

    def __init__(self, myArgument):
        self.classVar = myArgument

但是当我尝试像这样初始化我的类时:

c = myClass(argumentValue)

我得到这个错误:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: function takes 1 positional arguments but 2 were given

我做错什么了?你知道吗

在引用classVar之前是否需要声明它?你知道吗

编辑(已解决)

我能够找到错误,我注意到我在代码的其他地方定义了另一个__init__方法,它没有收到任何参数。你知道吗


Tags: 模块self声明参数initdef错误micropython
1条回答
网友
1楼 · 发布于 2024-04-27 02:55:06

你看过documentation about Python Object Creation吗?你知道吗

ISR’s cannot create instances of Python objects. This is because MicroPython needs to allocate memory for the object from a store of free memory block called the heap.

相关问题 更多 >