一个网友写的栈,问为啥不能迭代。具有__iter__ 和next方法的对象叫迭代器-七七巴巴黄页网
2013-09-27 08:26
471 查看
一个网友写的栈,问为啥不能迭代。具有__iter__ 和next方法的对象叫迭代器-七七巴巴黄页网
一个网友写的栈,问为啥不能迭代。具有__iter__ 和next方法的对象叫迭代器
python视频培训班
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 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 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)
python视频培训班
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 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 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)
相关文章推荐
- Python3基础 生成一个可以使用for循环可迭代的类,具有__iter__和__next__的方法的类
- 迭代器就是重复地做一些事情,可以简单的理解为循环,在python中实现了__iter__方法的对象是可迭代的,实现了next()方法的对象是迭代器,这样说起来有
- 具有__iter__ 和next方法的对象叫迭代器
- 迭代器-迭代对象-dir(a)可以查看该数据类型有多少种方法。range(10)在py3里就是一个迭代器,for循环实际就是迭代器的应用
- jq对象 dom对象的转换 为啥转换?因为jq和dom对象具有不同的方法可以使用
- 【一个实体对象不能由多个 IEntityChangeTracker 实例引用】原因及解决方法
- python 3-6 如何在一个for语句中迭代多个可迭代对象zip/itertools.chain
- 判断一个对象是否可迭代 的方法 分类: python 2015-03-27 12:20 82人阅读 评论(0) 收藏
- 定义一个“点”(Point)类用来表示三维空间中的点(有三个坐标)。要求如下: (1)可以生成具有特定坐标的点对象。 (2)提供可以设置三个坐标的方法。 (3)提供可以计算该“点”距原点距离平方的方法。 (4)编写主类程序验证。
- 判断一个对象是否可迭代 的方法
- .net C# 如何判断一个对象是否具有某个方法
- Spring AOP为什么不能拦截同一个对象内方法的嵌套调用
- python:判断一个对象是否可迭代 的方法
- C++中提供了多种基本的数据类型。实际上,这些远不能满足我们的需求,如复数(第10章的例子大多是处理虚数的),再如分数。本任务将设计一个简单的分数类,完成对分数的几个运算。一则巩固基于对象编程的方法,
- Spring AOP为什么不能拦截同一个对象内方法的嵌套调用(一)
- 为什么Iterator的remove方法可保证从源集合中安全地删除对象,而在迭代期间不能直接删除集合内元素
- Spark RDD编程(Python和Scala版本)----Spark中的RDD就是一个不可变的分布式对象集合,是一种具有兼容性的基于内存的集群计算抽象方法,Spark则是这个方法的抽象。 Spa
- JavaScript中hasOwnProperty函数方法是返回一个布尔值,指出一个对象是否具有指定名称的属性。
- Delphi函数指针的两种定义(对象方法存在一个隐藏参数self,所以不能相互赋值)
- python 3-1 如何实现可迭代对象iterable和迭代器对象iterator,__iter__,__getitem__