****反射****
#dir(obj,property|method)列出对象的属性名和方法名 #hasatter(obj,property|method)判断是否存在属性和方法 #getatter(obj,property|method,default)调用属性或方法 #setatter(obj,property|method,new)修改属性或方法
class MyOpen(object):
def __init__(self, filename, mode='r'):
self.name = filename
self.mode = mode
def get_name(self):
a = 2
print("get name info name......", end=' ')
return self.name
def get_mode(self):
return self.mode
open1 = MyOpen('/etc/passwd')
open2 = MyOpen('/etc/passwd')
print(dir(open1))
print(hasattr(open1, 'name'))
print(hasattr(open1, 'get_name'))
print(getattr(open1, 'name'))
print(getattr(open1, 'get_name')())
def aa():
return "this is add functions"
setattr(open1, 'name', 'sr132')
print(getattr(open1, 'name'))
setattr(open1, 'get_name', aa)
print(getattr(open1, 'get_name')())
#def __exit__(self, exc_type, exc_val, exc_tb):
# self.f.close()
类里面实现with语句
****异常处理****
try:
xxxxx尝试某操作
except xxx:
xxxxx出现某错误执行
finally:
xxxxx无论报不报错都执行
raise xxxxx抛出该错误
异常类:
BaseException
+-- SystemExit
+-- KeyboardInterrupt
+-- GeneratorExit
+-- Exception
+-- StopIteration
+-- StopAsyncIteration
+-- ArithmeticError
| +-- FloatingPointError
| +-- OverflowError
| +-- ZeroDivisionError
+-- AssertionError
+-- AttributeError
+-- BufferError
+-- EOFError
+-- ImportError
| +-- ModuleNotFoundError
+-- LookupError
| +-- IndexError
| +-- KeyError
+-- MemoryError
+-- NameError
| +-- UnboundLocalError
+-- OSError
| +-- BlockingIOError
| +-- ChildProcessError
| +-- ConnectionError
| | +-- BrokenPipeError
| | +-- ConnectionAbortedError
| | +-- ConnectionRefusedError
| | +-- ConnectionResetError
| +-- FileExistsError
| +-- FileNotFoundError
| +-- InterruptedError
| +-- IsADirectoryError
| +-- NotADirectoryError
| +-- PermissionError
| +-- ProcessLookupError
| +-- TimeoutError
+-- ReferenceError
+-- RuntimeError
| +-- NotImplementedError
| +-- RecursionError
+-- SyntaxError
| +-- IndentationError
| +-- TabError
+-- SystemError
+-- TypeError
+-- ValueError
| +-- UnicodeError
| +-- UnicodeDecodeError
| +-- UnicodeEncodeError
| +-- UnicodeTranslateError
+-- Warning
+-- DeprecationWarning
+-- PendingDeprecationWarning
+-- RuntimeWarning
+-- SyntaxWarning
+-- UserWarning
+-- FutureWarning
+-- ImportWarning
+-- UnicodeWarning
+-- BytesWarning
+-- ResourceWarning
"""
***Anaconda的安装和使用***
bash xxx.sh 安装
conda create --name env_name python==3.6 创建虚拟环境并指定python解释器版本
conda env list / conda info -e 列出已有虚拟环境
source activate env_name 进入某虚拟环境
source deactivate 退出环境
conda remove -name env_name -all 删除虚拟环境
conda list 列出该环境已下载的包
conda install package_name /yum/pip 给环境下载第三方包
conda search package_name在conda库里查找是否有该包
conda remove/update package_name 删除或更新某环境下的包