[关闭]
@demonly 2017-10-30T14:52:39.000000Z 字数 754 阅读 731

魔法方法、属性和迭代器

Python


property(fget, fset, fdel, doc) 接受四个方法,创建一个属性。

  1. class Rectangle:
  2. def __init__(self):
  3. self.width = 0
  4. self.height = 0
  5. def setSize(self, size):
  6. self.width, self.height = size
  7. def getSize(self):
  8. return self.width, self.height
  9. size = property(getSize, setSize)

__iter__ 为可迭代对象指定迭代器。
生成器中的等待点使用 yield 语句指定,迭代器通过 next 方法返回值,通过 send 方法传入值。

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注