查看: 1832| 回复: 0
跳转到指定楼层
上一主题 下一主题
收起左侧

[学Python/Perl] 利用ctypes实现array,迭代出错

全局:

注册一亩三分地论坛,查看更多干货!

您需要 登录 才可以下载或查看附件。没有帐号?注册账号

x
  1. import ctypes
  2. class  Array(object):
  3.     """docstring for  a"""
  4.     def __init__(self, size):
  5.         assert size > 0 and type(size) == int,'Array size must be >0 and a integer'
  6.         self._size = size
  7.         PyArrayType = ctypes.py_object*self._size
  8.         self._elements = PyArrayType()
  9.         self.clear(None)
  10.     def __len__(self):
  11.             return self._size
  12.     def __getitem__(self,index):
  13.             assert index >= 0 and index < self._size ,'Array sunscript out of range'
  14.             return self._elements[index]
  15.     def __setitem__(self,index,value):
  16.             assert index >= 0 and index < self._size ,'Array sunscript out of range'
  17.             self._elements[index] = value
  18.     def clear(self,value):
  19.             for i in range(self._size):
  20.                 self._elements[i] = value
  21.     def __iter__(self):
  22.             return _ArrayIterator(self._elements)
  23. class _ArrayIterator(object):
  24.     """docstring for _ArrayIterator"""
  25.     def __init__(self, theArray):
  26.         self._arrayRef = theArray
  27.         self._curNdx = 0
  28.     def __iter__(self):
  29.         return self
  30.     def __next__(self):
  31.         if self._curNdx >= 0 and self._curNdx < len(self._arrayRef):
  32.             entry = self._arrayRef[self._curNdx]
  33.             self._curNdx += 1
  34.             return entry
  35.         else:
  36.             raise StopIteration
复制代码
当进行以下操作时:
  1. a = Array(5)       
  2. a.clear(5)
  3. for i  in a:
  4.     print i
复制代码
出现iter() returned non-iterator of type '_ArrayIterator'
的报错,请大神指点一下

上一篇:LeetCode 一道题,求Binary Tree 最大和路径
下一篇:Amazon online test windowsum的题
您需要登录后才可以回帖 登录 | 注册账号
隐私提醒:
  • ☑ 禁止发布广告,拉群,贴个人联系方式:找人请去🔗同学同事飞友,拉群请去🔗拉群结伴,广告请去🔗跳蚤市场,和 🔗租房广告|找室友
  • ☑ 论坛内容在发帖 30 分钟内可以编辑,过后则不能删帖。为防止被骚扰甚至人肉,不要公开留微信等联系方式,如有需求请以论坛私信方式发送。
  • ☑ 干货版块可免费使用 🔗超级匿名:面经(美国面经、中国面经、数科面经、PM面经),抖包袱(美国、中国)和录取汇报、定位选校版
  • ☑ 查阅全站 🔗各种匿名方法

本版积分规则

>
快速回复 返回顶部 返回列表