我正在尝试在python中编辑库(perfmon)-文件为session.py我想添加一个模块,该模块可以记录来自USB端口的一些读数。这也是我对python类的第一次尝试from perfmon import *import osimport sysimport serial# Common base classclass Session: def __init__(self, events): self.system = System() self.event_names = events self.events = [] self.fds = [] for e in events: err, encoding = pfm_get_perf_event_encoding(e, PFM_PLM0 | PFM_PLM3, None, None) self.events.append(encoding) self.USB = serial.Serial("/dev/ttyUSB0",115200) def __del__(self): pass def read(self, fd): # TODO: determine counter width return os.read(fd, 8)class PerThreadSession(Session): def __init__(self, pid, events): self.pid = pid Session.__init__(self, events) def __del__(self): Session.__del__(self) def start(self): for e in self.events: self.fds.append(perf_event_open(e, self.pid, -1, -1, 0)) def read(self, i): return Session.read(self, self.fds[i])class USB(Session): def __init__(self): return self.USB这是我得到的以下错误:Python 2.7.3 (default, Apr 14 2012, 08:58:41) [GCC] on linux2Type "help", "copyright", "credits" or "license" for more information.>>> import perfmon>>> test = perfmon.USB()Traceback (most recent call last): File "<stdin>", line 1, in <module>AttributeError: 'module' object has no attribute 'USB'我在做什么错?我了解self在python中充当构造函数,我可以在其中返回值吗?
2 回答
慕无忌1623718
TA贡献1744条经验 获得超4个赞
据我了解,在您的情况下,答案是
>>> from perfmon import session
>>> test = session.USB()
但是这种方式,你的举动是错误的。您应该在工作区/项目目录中创建自己的模块。导入会话模块并在那里创建类
from perfmon import session
class USB(Session):
def __init__(self):
pass #I have changed this part as It has two errors in your example
添加回答
举报
0/150
提交
取消