VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > 编程开发 > python3 >
  • python3教程之Python 中__new__方法详解及使用(2)

本站最新发布   Python从入门到精通|Python基础教程
试听地址  
https://www.xin3721.com/eschool/python.html


 

Python3的写法

1
2
3
4
5
6
class Singleton(object):
    def __new__(cls,*args, **kwargs):
        if not hasattr(cls,'_inst'):
            print(cls)
            cls._inst = super(Singleton, cls).__new__(cls)
        return cls._inst

如果Python3的写法跟Python2写法一样,那么倒数第二行会报错"TypeError: object() takes no parameters"

相关教程