您的位置:首页 > 编程语言 > Python开发

Python学习异常检测

2017-08-21 18:05 211 查看
# -*- coding: utf-8 -*-

"""

Created on Mon Aug 21 14:11:22 2017

@author: cq

"""

#python处理异常

try :

    2/0

except Exception,e: #其中e为实例,即错误的实况

    print(e)

    

    

try:

    a = 1/0

except Exception,e:

    print e

finally:

    print 'hehehe'

    

    

try:

    a = 1/2

except Exception,e:

    print e

else:

    print 'success'

    

    

try:

    1/0

except IndexError:

    print 'qqq'

except ZeroDivisionError:

    print 'eeee'

    

finally:

    print('you will die')

    

  #学习生成器

def fib(max):

    a,b=1,1

    while a<max:

        yield a #generators return an iterator that returns a stream of values

        a,b=b,a+b

for n in fib(15):

    print(n)

    

   #学习私有属性,私有属性在外界是访问不了的

class Person:

    def __init__(self):

        self.__name='haha'#私有属性

        self.age=22

    def __get_name(self):#私有方法

        return self.__name

    def get_age(self):

        return self.age

    

person=Person()

print(person.get_age())

print(person.__get_name())

    

#学习time

import time 

time.time()

time.localtime(time.time())

time.strftime('%Y-%m-%d',time.localtime(time.time()))

time.localtime(time.time())[:3]
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: