python Class: 面向对象高级编程 __iter__ 和 next()

官网解释:

New in version 2.2.

  • iterator.__iter__()
  • Return the iterator object itself. This is required to allow both containers and iterators to be used with the for and instatements. This method corresponds to the tp_iter slot of the type structure for Python objects in the Python/C API.
  • iterator.next()
  • Return the next item from the container. If there are no further items, raise the StopIteration exception. This method corresponds to the tp_iternext slot of the type structure for Python objects in the Python/C API.

也就是说 __iter__与next()是配套使用的。

Fibonacci数列:

#!/usr/bin/python

# -*- coding: utf-8 -*-

class Fibo(object):

def __init__(self):

self.a, self.b = 0, 1

def __iter__(self):

return self

def __next__(self):

self.a, self.b = self.b, self.a + self.b

if self.a > 10000:

raise StopIteration()

return self.a, self.b

for n in Fibo():

print n

运行失败:

怎么用class做才能成功呢?????

后来,才发现,我2.7版本的解释器不支持,用网页上的Python3在线编程解释器完美运行。。。。。。is ri le gou le.

附使用的python3环境:http://www.dooccn.com/python3/

请问各位大佬,在2.7版本中我该怎么使用 __iter__ 呢??求教!!!

原文地址:http://blog.51cto.com/13502993/2147531

时间: 2024-08-19 06:55:31

python Class: 面向对象高级编程 __iter__ 和 next()的相关文章

python基础-面向对象高级编程

实例化对象绑定属性 s.name = 'Michael' # 动态给实例绑定一个属性 类绑定方法---所有实例化对象均可调用Student.set_score = set_score 实例化对象绑定方法---只有该对象可调用from types import MethodType#绑定方法需要使用MethodTypes.set_age = MethodType(set_age, s) # 给实例绑定一个方法 Python内置的@property装饰器就是负责把一个方法变成属性调用的把一个gett

python 之面向对象高级编程

数据封装.继承和多态只是面向对象程序设计中最基础的3个概念.在Python中,面向对象还有很多高级特性,允许我们写出非常强大的功能. 我们会讨论多重继承.定制类.元类等概念. 使用__slots__ 正常情况下,当我们定义了一个class,创建了一个class的实例后,我们可以给该实例绑定任何属性和方法,这就是动态语言的灵活性.先定义class: class Student(object): pass 然后,尝试给实例绑定一个属性: >>> s = Student() >>&

python之面向对象高级编程

@property: 既能检查参数,又可以用类似属性这样简单的方式来访问类的变量,可以让调用者写出简短的代码 class Student(object): #birth是可读写属性(多定义了一个setter的装饰器),而age就是一个只读属性 @property def birth(self): return self._birth @birth.setter def birth(self, value): self._birth = value @property def age(self):

Python学习---面向对象高级编程

Python允许在定义class的时候,定义一个特殊的__slots__变量,来限制该class实例能添加的属性: class Student(object): __slots__ = ('name', 'age') # 用tuple定义允许绑定的属性名称使用__slots__要注意,__slots__定义的属性仅对当前类实例起作用,对继承的子类是不起作用的.除非在子类中也定义__slots__,这样,子类实例允许定义的属性就是自身的__slots__加上父类的__slots__. Python

【Python】[面向对象高级编程] 使用__slots__,使用@property

1.使用 __slots__    给实例绑定方法, >>> def set_age(self, age): # 定义一个函数作为实例方法 ... self.age = age ... >>>from types import MethodType >>>s.set_age=MethodType(set_age,s) >>>s.set_age(25) >>>s.age 25 为了给所有的实例都绑定方法,可以给类绑定方

Python3 面向对象 高级编程

正常情况下,当我们定义了一个class,创建了一个class的实例后,我们可以给该实例绑定任何属性和方法,这就是动态语言的灵活性.  class Student(object): pass 然后,尝试给实例绑定一个属性: >>> s = Student() >>> s.name = 'Michael' # 动态给实例绑定一个属性 还可以尝试给实例绑定一个方法: >>> def set_age(self, age): # 定义一个函数作为实例方法 ...

[7]面向对象高级编程

[7]面向对象高级编程 数据封装.继承和多态只是面向对象程序设计中最基础的3个概念.在Python中,面向对象还有很多高级特性,允许我们写出非常强大的功能. 我们会讨论多重继承.定制类.元类等概念. 使用__slots__ 正常情况下,当我们定义了一个class,创建了一个class的实例后,我们可以给该实例绑定任何属性和方法,这就是动态语言的灵活性.先定义class: >>> class Student(object): ... pass ... 然后,尝试给实例绑定一个属性: >

C++面向对象高级编程(九)Reference与重载operator new和operator delete

摘要: 技术在于交流.沟通,转载请注明出处并保持作品的完整性. 一 Reference 引用:之前提及过,他的主要作用就是取别名,与指针很相似,实现也是基于指针. 1.引用必须有初值,且不能引用nullptr 2.引用之后不能再引用别人 3.引用通常不用于声明变量,多用于参数类型,和返回值类型 见下面代码 int main(int argc, const char * argv[]) { int x=0; //p is a pointer to x int* p = &x; // r is a

C++面向对象高级编程(七)point-like classes和function-like classes

技术在于交流.沟通,转载请注明出处并保持作品的完整性. 1.pointer-like class 类设计成指针那样,可以当做指针来用,指针有两个常用操作符(*和->),所以我们必须重载这两个操作 /*简单实现一下智能指针的*与 -> 操作符*/ template <class T> class shared_ptr_test { public: T& operator* () const //重载* { return *px; } T* operator->() co