一个网友写的栈,问为啥不能迭代。具有__iter__ 和next方法的对象叫迭代器-七七巴巴黄页网
一个网友写的栈,问为啥不能迭代。具有__iter__ 和next方法的对象叫迭代器-七七巴巴黄页网
一个网友写的栈,问为啥不能迭代。具有__iter__ 和next方法的对象叫迭代器
class stack(object):
def __init__(self):
self.stack = []def push(self,str1):
self.stack.append(str1)def pop(self):
return self.stack.pop()def length(self):
return len(self.stack)请问应该怎么遍历这个堆栈呢?
请看迪艾姆公司python远程视频培训班黄哥的回答
类只有实现了__iter__() 和next()方法(python3改为__next__()),生成的实例才能迭代
具有__iter__ 和next方法的对象叫迭代器
请看python2.7.5的代码
#coding:utf-8
"""
该代码由python远程培训班黄哥所写
python远程培训http://www.qy7788.com.cn/shiyongxinxi/shiyongxinxi193.html
qq:1465376564 tel:010-68165761
"""
class stack(object):
"""只有实现了__iter__和next方法的类生成的实例才可以迭代"""
def __init__(self):
self.stack = []def push(self,str1):
self.stack.append(str1)def pop(self):
return self.stack.pop()def length(self):
return len(self.stack)def __iter__(self):
return selfdef __next__(self):
try:
return self.stack.pop()
except IndexError:
raise StopIterations = stack()
s.push("python远程培训")
s.push("qq:1465376564 tel:010-68165761")
s.push("python编程思路")
for i in s:
print i请看python3.3.2的代码
#coding:utf-8
"""该代码由python远程培训班黄哥所写
python远程培训http://www.qy7788.com.cn/shiyongxinxi/shiyongxinxi193.html
qq:1465376564 tel:010-68165761 在python3.3.2环境下测试过 """
class stack(object):
"""只有实现了__iter__和next方法的类生成的实例才可以迭代"""
def __init__(self):
self.stack = []
def push(self,str1):
self.stack.append(str1)
def pop(self):
return self.stack.pop()
def length(self):
return len(self.stack)
def __iter__(self):
return self
def __next__(self):
try:
return self.stack.pop()
except IndexError:
raise StopIteration
s = stack()
s.push("python远程培训")
s.push("qq:1465376564 tel:010-68165761")
s.push("python编程思路")
for item in s:
print(item)
一个网友写的栈,问为啥不能迭代。具有__iter__ 和next方法的对象叫迭代器-七七巴巴黄页网的更多相关文章
- 从零开始写STL—栈和队列
从零开始写STL-栈和队列 适配器模式 意图:将一个类的接口转换成客户希望的另外一个接口.适配器模式使得原本由于接口不兼容而不能一起工作的那些类可以一起工作. 主要解决:主要解决在软件系统中,常常要将 ...
- springmvc2 一个控制器写多个方法(非注解方式)
出处:http://blog.csdn.net/xuewenke/article/details/23895999 springmvc2 一个控制器写多个方法(非注解方式) 分类: spring 20 ...
- SpringMVC实现一个controller写多个方法
MultiActionController与ParameterMethodNameResolver在一个Controller类中定义多个方法,并根据使用者的请求来执行当中的某个方法,相当于Struts ...
- PHP的排列组合问题 分别从每一个集合中取出一个元素进行组合,问有多少种组合?
首先说明这是一个数学的排列组合问题C(m,n) = m!/(n!*(m-n)!) 比如:有集合('粉色','红色','蓝色','黑色'),('38码','39码','40码'),('大号','中号') ...
- 设单链表中存放n个字符,试设计一个算法,使用栈推断该字符串是否中心对称
转载请注明出处:http://blog.csdn.net/u012860063 问题:设单链表中存放n个字符.试设计一个算法,使用栈推断该字符串是否中心对称,如xyzzyx即为中心对称字符串. 代码例 ...
- 分享一个c#写的开源分布式消息队列equeue
分享一个c#写的开源分布式消息队列equeue 前言 equeue消息队列中的专业术语 Topic Queue Producer Consumer Consumer Group Broker 集群消费 ...
- 分享一个自己写的MVC+EF “增删改查” 无刷新分页程序
分享一个自己写的MVC+EF “增删改查” 无刷新分页程序 一.项目之前得添加几个组件artDialog.MVCPager.kindeditor-4.0.先上几个效果图. 1.首先建立一个数 ...
- EQueue - 一个C#写的开源分布式消息队列的总体介绍(转)
源: EQueue - 一个C#写的开源分布式消息队列的总体介绍 EQueue - 一个纯C#写的分布式消息队列介绍2 EQueue - 详细谈一下消息持久化以及消息堆积的设计
- vue element-ui怎样提炼一个自己写的js当作公共js
vue element-ui怎样提炼一个自己写的js当作公共js请教一下各位大神,我刚刚触摸vue element-ui几天,写的一个清晰检索的input框,现在需当作项目公共的部分,可遭需的html ...
随机推荐
- 一些Xcode 5的使用提示和技巧
摘自:http://www.cocoachina.com/newbie/env/2014/0127/7766.html 感谢论坛成员郭亚鑫的热心翻译. 在iOS开发中,Xcode 是最使用最多的I ...
- ERROR 1290 (HY000): The MySQL server is running with the --skip-grant-tables opt
mysql跳过权限: mysqld -nt --skip-grant-tables opt 登录:mysql -uroot -p 修改root密码 set password for 'root'@'l ...
- CF 319C(Kalila and Dimna in the Logging Industry-斜率DP,注意叉积LL溢出)
C. Kalila and Dimna in the Logging Industry time limit per test 2 seconds memory limit per test 256 ...
- 在WINDOWS下 三步快速配置 eclipse c++ 环境
所需软件 1.Eclipse IDE for C/C++ Developers http://www.eclipse.org/downloads/packages/eclipse-ide-cc-dev ...
- MFC 只启动一个程序实例
问题描述: 我们开发过程中可能会经常遇到,只启动一个程序实例.即一个程序启动之后,如果再次执行该程序,将会恢复之前打开的程序,而不是打开一个新的程序. 实现原理:利用FindWindow/FindWi ...
- BZOJ 1342: [Baltic2007]Sound静音问题( 单调队列 )
一开始写了个RMQ然后就T了... 好吧正解是单调队列, 维护两个单调队列... ----------------------------------------------------------- ...
- SQL模板和模板实例化
需求:需要得出一个数据源DataTable,我已知SQL和HttpRequest如何,通过SQL模板的方式去实例化匹配HttpRequest中的参数实例化为查询SQL,最后返回DataTable 1. ...
- 在JS中,一个自定义函数如何调用另一个自定义函数中的变量
function aa1511() { var chengshi="马鞍山"; var shengfen="安徽省"; return shengfen+&quo ...
- 第二代map-reduce架构YARN解析
需求 我们在考虑hadoop map-reduce框架的时候,最重要需包括: 1. reliability 可靠性,主要是jobtracker,resource manager可靠性 2. avail ...
- Mac 实用工具
命令行常用工具: Iterm2 也是一个终端命令行工具,支持多工作区,使用清爽 http://www.iterm2.com/documentation.html 给你的命令行 代码上色 Solariz ...