python第五节

  1. python中的构造方法

__new__(cls,*args,**kwargs):必须有返回值,是类方法

__init__():此为初始化方法

  1. mro(),可以看到所有父类,即搜索顺序

@classmethod  类方法

__method__(class)

  1. 单例

Class SingleTone:

   __instance=None

   def __new__(cls):

      if cls.__instance==None:

        cls.__instance=object.__new__(cls)

        return cls.__instance

      else:

扫描二维码关注公众号,回复: 1460678 查看本文章

        return cls.__instance

s=SingleTone()

ss=SingleTone()

print(id(s),id(ss))

  1. 异常

try:

语句体

Except

异常

Try:

语句体

Except(异常类型1,异常类型2):

except Exception:

所有异常

Try:

Except 异常类型 as 名称:

Print(名称)

Else 

猜你喜欢

转载自www.cnblogs.com/TreasureQiu/p/9136778.html