标签:sof name cto cst 类型 com 斐波那契数 ast recent
>>> open("notexist.txt") Traceback (most recent call last): File "<stdin>", line 1, in <module> FileNotFoundError: [Errno 2] No such file or directory: ‘notexist.txt‘
>>> raise FileNotFoundError
try: open(r"c:\Users\kingsoft\Desktop\notexist.txt") except FileNotFoundError as e: print ("file not exist...") except (name1,name2): print ("io error is true...") else: print ("file exist..") finally: print ("always do...")
python是完全面向对象的,python中一切都是对象,包括变量,函数等。
class MyException(): pass
class Person(): def __init__(self, name): self.name = name def sayname(self): print self.name m = Person("joe") print(m.sayname())
class Person(): def __init__(self, name): self.name = name Person.name = name def sayname(self): print("myname is :" + self.name) print("myexceptionname is: " + Person.name) def changeothername(self, name): Person.name = name def __del__(self): print(self.name + "is gone") m = Person("joe") m.sayname() print("m.name : " + m.name) m.test="tt" print(m.test) j = Person("jason") j.sayname() j.changeothername(j.name) m.sayname()
class Fibs(object): """docstring for Fibs""" def __init__(self, max): self.max = max self.a = 0 self.b = 1 def __next__(self): fib = self.a if fib > self.max: raise StopIteration self.a, self.b = self.b, self.a + self.b return fib #返回迭代器 def __iter__(self): return self fib = Fibs(1000) for f in fib: print(f, end= " ")
#斐波那契数列 def getfibs(max): a = 0 b = 1 while a < max: a, b= b, a+b value = a yield value print(getfibs(1000)) for i in getfibs(1000): print(i)
def gen(): yield "hello" yield "how" yield "are" yield "you" for i in gen(): print(i)
标签:sof name cto cst 类型 com 斐波那契数 ast recent
原文地址:http://www.cnblogs.com/crazymanpj/p/6849932.html