python 魔法方法,属性,迭代

9.2 构造方法

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

class fibs:
	def __init__(self):
		self.a = 0
		self.b = 1
	def next(self):
		self.a,self.b = self.b,self.a+self.b
		return self.a
	def __iter__(self):
		return self

	
fib = fibs()
for i in fib:
    if i>1000:
        print i
        break

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


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

class Bird:
    def __init__(self):
        self.hungry = True
    def eat(self):
        if self.hungry:
            print 'Ohhh.haha!'
            self.hungry = False
        else:
            print 'I\'m full Thanks!'
class SongBird(Bird):
    def __init__(self):
        self.sound = 'Squawk!'
    def sing(self):
        print self.sound

b = Bird()
b.eat()
b.eat()
sb = SongBird()
sb.sing()
sb.eat()

>>> 
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'
>>> 

class Bird:
    def __init__(self):
        self.hungry = True
    def eat(self):
        if self.hungry:
            print 'Ohhh.haha!'
            self.hungry = False
        else:
            print 'I\'m full Thanks!'
class SongBird(Bird):
    def __init__(self):
        self.sound = 'Squawk!'
        Bird.__init__(self)
    def sing(self):
        print self.sound

b = Bird()
b.eat()
b.eat()
sb = SongBird()
sb.sing()
sb.eat()
>>> 
Ohhh.haha!
I'm full Thanks!
Squawk!
Ohhh.haha!
>>> 

# -*- coding: cp936 -*-
class Bird:
    def __init__(self):
        self.hungry = True
    def eat(self):
        if self.hungry:
            print 'Ohhh.haha!'
            self.hungry = False
        else:
            print 'I\'m full Thanks!'
    @classmethod
    def say(self):
        print 'Hello ,everybody!'
    #类中的方法都至少带个self参数
    #这样声明的函数只用类对象才能调用
    #只有@classmethod 或@staticmethod 可用类名调用,而不需要生成实例对象
class SongBird(Bird):
    def __init__(self):
        self.sound = 'Squawk!'
        Bird.__init__(self)
    def sing(self):
        print self.sound

b = Bird()
b.eat()
b.eat()
b.say()
Bird.say()
sb = SongBird()
sb.sing()
sb.eat()

>>> 
Ohhh.haha!
I'm full Thanks!
Hello ,everybody!
Hello ,everybody!
Squawk!
Ohhh.haha!
>>> 

9.7递归生成器

# -*- coding: cp936 -*-
def flatten(nested):
    try:
        for sublist in nested:
            for element in flatten(sublist):
                yield element
    except TypeError:
        yield nested
def flatten_new(nested):
    try:
        try:nested + ''#列表不能加字符
        except:pass
        else:raise TypeError
        for sublist in nested:
            for element in flatten_new(sublist):
                yield element
    except TypeError:
        yield nested
test = [[[1],2],[3],[4,[5,[6,[7]]],8]]
test_char = ['foo',['bar',['join']],'tu']
print "old\n:",list(flatten(test))
print "new\n:",list(flatten_new(test))
print "new\n:",list(flatten_new(test_char))
print "old\n:",list(flatten(test_char))#死循环
old
: [1, 2, 3, 4, 5, 6, 7, 8]
new
: [1, 2, 3, 4, 5, 6, 7, 8]
new
: ['foo', 'bar', 'join', 'tu']
old
:
Traceback (most recent call last):
  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):
  File "G:/New Knowledge/practice/python/yield_test.py", line 5, in flatten
    for element in flatten(sublist):


>>> def repeater(value):
	while True:
		new = (yield value)
		if new is not None:value = new

>>> r = repeater(43)
>>> r.next()
43
>>> r.next()
43
>>> r.send("hello!")
'hello!'
>>> r.next()
'hello!'
>>> 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值