天天看点

python 魔法方法,属性,迭代

9.2 构造方法

python 中也属于自己的构造函数和析构函数,

其中的__init__ 就是在类创建对象的时候自动调用的,而下面两个函数就是在迭代的时候自动调用的,这是python实现的语法。

类在继承时不显示调用父类的构造方法时,父类构造方法中声明的元素不会被继承

>>> 

ohhh.haha!

i‘m full thanks!

squawk!

traceback (most recent call last):

  file "g:\new knowledge\practice\python\test.py", line 21, in <module>

    sb.eat()

  file "g:\new knowledge\practice\python\test.py", line 5, in eat

    if self.hungry:

attributeerror: songbird instance has no attribute ‘hungry‘

hello ,everybody!

9.7递归生成器

old

: [1, 2, 3, 4, 5, 6, 7, 8]

new

: [‘foo‘, ‘bar‘, ‘join‘, ‘tu‘]

:

  file "g:/new knowledge/practice/python/yield_test.py", line 24, in <module>

    print "old\n:",list(flatten(test_char))

  file "g:/new knowledge/practice/python/yield_test.py", line 5, in flatten

    for element in flatten(sublist):

继续阅读