xlwarkey2.4,年轮网络日记,rgb颜色表
class a(object): def add(self, a,b ): return a+b count = a() print(count.add(3,5))
class a(): def __init__(self,a,b): self.a = int(a) self.b =int(b) def add(self): return self.a+self.b count = a('4',5) print(count.add())
9
class a(): def add(self, a, b): return a+b class b(a): def sub(self, a,b): return a-b print(b().add(4,5))
9
也叫类库和模块。
import...或from ... import...来引用模块
import time print (time.ctime())
wed nov 7 16:18:07 2018
只引用ctime()方法
from time import ctime print(ctime())
wed nov 7 16:19:53 2018
全部引用
from time import *
from time import * print(ctime()) print("休眠两秒") sleep(2) print(ctime())
wed nov 7 16:26:37 2018
休眠两秒
wed nov 7 16:26:39 2018
目录样式
project/ pub.py count.py
pub.py
def add(a,b) return a +b
count.py
from pub import add print(add(4,5))
9
目录样式
project model pub.py count.py
from model.pub import add print add(4,5)
--
这里面有多级目录的还是不太了解,再看一下
open('abc.txt','r') #报异常
python3.0以上
try: open('abc.txt','r') except filenotfounderror: print("异常")
python2.7不能识别filenotfounderror,得用ioerror
try: open('abc.txt','r') except ioerror: print("异常")
try: print(abc) except nameerror: print("异常")
所有的异常都继承于exception
try: open('abc.txt','r') except exception: print("异常")
exception继承于baseexception。
也可以使用baseexception来接收所有的异常。
try: open('abc.txt','r') except baseexception: print("异常")
try: open('abc.txt','r') print(aa) except baseexception as msg: print(msg)
[errno 2] no such file or directory: 'abc.txt'
try: aa = "异常测试" print(aa) except exception as msg: print (msg) else: print ("good")
异常测试
good
还可以使用 try... except...finally...
try: print(aa) except exception as e: print (e) finally: print("always do")
name 'aa' is not defined
always do
from random import randint #生成随机数 number = randint(1,9) if number % 2 == 0: raise nameerror ("%d is even" %number) else: raise nameerror ("%d is odd" %number)
traceback (most recent call last):
file "c:/python27/raise.py", line 8, in
如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复
新手学习Python2和Python3中print不同的用法
Python基于os.environ从windows获取环境变量
网友评论