Python基础语法(3)——文件流、异常处理、面向对象编程以及装饰器
2017-03-04 15:40
741 查看
10 输入输出
10.1 输入输出方式介绍
可采用input方式接收控制台的输入
str1=input("Please
input a string:")
[align=left]print(str1)[/align]
print("{}".format(str1))
10.2 IO文件流
写文件
# -*- coding=utf-8 -*-
[align=left]textContext='''\[/align]
[align=left]Created on 2017年2月26日[/align]
@author:
ZhuangLiang
[align=left]'''[/align]
f=open("text.txt",
"w")
[align=left]f.write(textContext)[/align]
f.close()
读文件
[align=left]f=open("text.txt")[/align]
while
True:
str=f.readline()
if
len(str)==0:
break
print(str)
11 异常处理
11.1 错误与异常处理
1 语法错误(Syntax Errors)
2 异常(Exceptions)
while
True:
try:
int(input("Enter
an number:"))
break
except
ValueError:
print("you
input the invalid number!")
[align=left]try:[/align]
[align=left] f=open("number.txt")[/align]
[align=left] s=f.readline()[/align]
[align=left] num=int(s.strip())[/align]
except
OSError
as
err:
print("OSError:",err)
except
ValueError:
print("can
not convert into integer")
12 面向对象编程(Objected-Oriented)及装饰器(decorator)
12.1 面向对象编程
class
Student:
def
__init__(self,name,age):
self.name=name
self.age=age
def
introduce(self):
print("I'm
",self.name)
print("I'm
"+str(self.age)+"
years old!")
def
updateAge(self,newAge):
self.age=newAge
[align=left]jim=Student("liangzhuang",24)[/align]
[align=left]jim.introduce()[/align]
[align=left]jim.updateAge(28)[/align]
print(jim.age)
12.2 装饰器
装饰函数以接收函数名参数,并且返回函数名,调用装饰函数后得到的函数是经过"装饰"的函数,示例如下:
def
deco(func):
def
inFunc():
return
"inFunc: "+func()
return
inFunc
#
@deco
def
myfunc():
return
"myfunc
called."
[align=left]myfunc=deco(myfunc)[/align]
print(myfunc())
一般为了程序简洁,可采用注解的方式装饰函数,如下:
def
deco(func):
def
inFunc():
return
"inFunc: "+func()
return
inFunc
[align=left]@deco[/align]
def
myfunc():
return
"myfunc
called."
#
myfunc=deco(myfunc)
[align=left]print(myfunc())[/align]
[align=left]
[/align]
10.1 输入输出方式介绍
可采用input方式接收控制台的输入
str1=input("Please
input a string:")
[align=left]print(str1)[/align]
print("{}".format(str1))
10.2 IO文件流
写文件
# -*- coding=utf-8 -*-
[align=left]textContext='''\[/align]
[align=left]Created on 2017年2月26日[/align]
@author:
ZhuangLiang
[align=left]'''[/align]
f=open("text.txt",
"w")
[align=left]f.write(textContext)[/align]
f.close()
读文件
[align=left]f=open("text.txt")[/align]
while
True:
str=f.readline()
if
len(str)==0:
break
print(str)
11 异常处理
11.1 错误与异常处理
1 语法错误(Syntax Errors)
2 异常(Exceptions)
while
True:
try:
int(input("Enter
an number:"))
break
except
ValueError:
print("you
input the invalid number!")
[align=left]try:[/align]
[align=left] f=open("number.txt")[/align]
[align=left] s=f.readline()[/align]
[align=left] num=int(s.strip())[/align]
except
OSError
as
err:
print("OSError:",err)
except
ValueError:
print("can
not convert into integer")
12 面向对象编程(Objected-Oriented)及装饰器(decorator)
12.1 面向对象编程
class
Student:
def
__init__(self,name,age):
self.name=name
self.age=age
def
introduce(self):
print("I'm
",self.name)
print("I'm
"+str(self.age)+"
years old!")
def
updateAge(self,newAge):
self.age=newAge
[align=left]jim=Student("liangzhuang",24)[/align]
[align=left]jim.introduce()[/align]
[align=left]jim.updateAge(28)[/align]
print(jim.age)
12.2 装饰器
装饰函数以接收函数名参数,并且返回函数名,调用装饰函数后得到的函数是经过"装饰"的函数,示例如下:
def
deco(func):
def
inFunc():
return
"inFunc: "+func()
return
inFunc
#
@deco
def
myfunc():
return
"myfunc
called."
[align=left]myfunc=deco(myfunc)[/align]
print(myfunc())
一般为了程序简洁,可采用注解的方式装饰函数,如下:
def
deco(func):
def
inFunc():
return
"inFunc: "+func()
return
inFunc
[align=left]@deco[/align]
def
myfunc():
return
"myfunc
called."
#
myfunc=deco(myfunc)
[align=left]print(myfunc())[/align]
[align=left]
[/align]
相关文章推荐
- python基础知识整理——错误以及异常处理
- 异常基础、以及异常处理框架资料整理
- 黑马程序员 Java基础 处理异常信息以及自定义异常
- Python中的异常处理相关语句基础学习笔记
- python基础教程共60课-第36课异常处理
- python基础之异常处理
- python语法31[异常处理]
- Python基础--正则表达式基本语法以及re模块
- Python中的异常处理相关语句基础学习笔记
- Python基础--正则表达式基本语法以及re模块
- 用python处理文本,本地文件系统以及使用数据库的知识基础
- Python异常处理方面的一些基础知识。
- Python基础教程之正则表达式基本语法以及re模块
- Python基础知识(三)--基本的异常处理、算术运算符、输入/输出
- .Net程序员之Python基础教程学习----函数和异常处理[Fifth Day]
- 开发常见错误解决(6)WSE3.0未处理的WebException,未处理的Web异常,基础连接以及关闭
- python异常以及面向对象编程
- Python基础语法以及在windows下如何导入模块
- python语法31[异常处理]
- 开发常见错误解决(6)WSE3.0未处理的WebException,未处理的Web异常,基础连接以及关闭