异常处理及反射

****反射****

#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 删除或更新某环境下的包



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值