1 __new__方法 创建对象

实质是:调用父类的__new__方法创建的对象

class Dog(object):
def __init__(self):
print("---init方法") def __str__(self):
print("---str方法") def __del__(self):
print("---init方法") def __new__(cls):
print("---new方法") dog1 = Dog()
---new方法     #只是创建了类对象,没有初始化,没有实例化对象

  2)版本2: cls此时是Dog指向的那个类对象

class Dog(object):
def __init__(self):
print("---init方法") def __str__(self):
print("---str方法") def __del__(self):
print("---init方法") def __new__(cls):   
print("---new方法")
print(id(cls)) print(id(Dog))
dog1 = Dog()
38097176
---new方法
38097176

  3)版本3:调用 被重写的方法__new__  创建对象

class Dog(object):
def __init__(self):
print("---init方法") def __str__(self):
print("---str方法") def __del__(self):
print("---del方法") def __new__(cls):
print("---new方法") #对父类的方法重写
# print(id(cls))
object.__new__(cls) #继续调用 被重写的方法 创建对象 #print(id(Dog))
dog1 = Dog()
---new方法
---del方法 #没有执行初始化

  4)版本4  __init__ 只负责初始化

class Dog(object):
def __init__(self):
print("---init方法") def __str__(self):
print("---str方法") def __del__(self):
print("---del方法") def __new__(cls): #cls此时是Dog指向的那个类对象
print("---new方法")
# print(id(cls))
return object.__new__(cls) #print(id(Dog))
dog1 = Dog()
---new方法
---init方法
---del方法

  5)版本5:优化版

class Dog(object):
#初始化 对象一创建开始执行
def __init__(self):
print("---init方法") def __str__(self):
print("---str方法")
return "返回对象的描述信息" #对象内存销毁时执行
def __del__(self):
print("---del方法") #通过父类的__new__方法创建对象
def __new__(cls): #cls此时是Dog指向的那个类对象
print("---new方法")
# print(id(cls))
return object.__new__(cls) #print(id(Dog))
dog1 = Dog()

    

相当于做了3件事情

1.调用__new__方法来创建对象,然后找了一个变量来接受__new__的返回值,这个返回值表示创建出来的对象的引用

2. __init__(刚刚创建出来的对象的引用)

3. 返回对象的引用
构造方法
__new__只负责创建
__init__ 只负责初始化 __new__ 和 __init__相当于C语言的构造方法

2. 创建单例对象

  1)什么是单例

      

class Dog(object):
pass dog1 = Dog()
dog2 = Dog()
print(id(dog1))
print(id(dog2))
139888234067784        #不是同一块内存  不是同一个对象
139888234068008

  2)版本2:

class Dog(object):
def __new__(cls):
if xxx :
return object.__new__(cls)
else:
return 上一次创建的对象的引用 dog1 = Dog()
dog2 = Dog()

 

   3)版本3:flag标志

class Dog(object):
__flag = 0
def __new__(cls):
if flag == 0:
flag = 1
return object.__new__(cls)
else:
xxxxx dog1 = Dog()
dog2 = Dog()

  4)版本4:类属性 实现

class Dog(object):
__flag = 0 #类属性 当做标志位 类属性是公有的
__instance = 0 #类属性 存储 上一次创建对象的引用
def __new__(cls): #这是一个类方法,cls指向类对象cls
if cls.__flag == 0:
cls.__flag = 1
cls.__instance = object.__new__(cls)
return cls.__instance
else:
return cls.__instance dog1 = Dog()
dog2 = Dog()
     #类方法
@classmethod
def add_num(cls): #cls指向类对象
cls.num += 1
     #实例方法
def __init__(self,new_name):
self.name = new_name #self.name 是实例属性 ,实例对象独有的
#对类属性 +1
Dog.num += 1

  5)版本5:__instance = None

class Dog(object):
__instance = None
def __new__(cls):
if cls.__instance == None:
cls.__instance = object.__new__(cls)
return cls.__instance
else:
return cls.__instance dog1 = Dog()
dog2 = Dog()

  6)版本6:查看对应的内存id

class Dog(object):
__instance = None
def __new__(cls):
if cls.__instance == None:
cls.__instance = object.__new__(cls)
return cls.__instance
else:
return cls.__instance dog1 = Dog()
dog2 = Dog()
print(dog1)
print(dog2)
python@ubuntu:~/pythonS6/python基础092$ python3 08-单例对象.py
<__main__.Dog object at 0x7f46d4584828>
<__main__.Dog object at 0x7f46d4584828>

  7)版本7:只初始化一次对象

 class Dog(object):
__instance = None
def __new__(cls,name):
if cls.__instance == None:
cls.__instance = object.__new__(cls)
return cls.__instance
else:
return cls.__instance def __init__(self,new_name):
self.name = new_name dog1 = Dog("tom") #单例对象 name='tom'
print(id(dog1))
print(dog1.name)
dog2 = Dog("jack") #同一对象 令name='jack'
print(id(dog2))
print(dog2.name)
140546425677920
tom
140546425677920
jack #如何让对象都是tom

      

  8)版本8:添加flag

class Dog(object):
__instance = None
__flag = False
def __new__(cls,name):
if cls.__instance == None:
cls.__instance = object.__new__(cls)
return cls.__instance
else:
return cls.__instance def __init__(self,new_name):
if Dog.__flag == False:
self.name = new_name
Dog.__flag = True dog1 = Dog("tom")
print(id(dog1))
print(dog1.name) #对象里面name = “tom”
dog2 = Dog("jack")
print(id(dog2))
print(dog2.name)
139813073586272
tom
139813073586272
tom #只初始化了一次对象

day 7 __new___的更多相关文章

  1. 面向对象的封装、继承和多态特性_python

    一.面向对象的几个特点 面向对象也称为类,拥有下面几个特点 1.封装特性:利用类的__init__(self)构造方法封装对象 构造方法:__init__(self):在生成对象的时候会自动调用 例子 ...

随机推荐

  1. 在Windows 10中更改网络连接优先级

    查看接口列表 (也可使用 如下) 选择网络连接,然后单击右侧的箭头以更改网络连接优先级.  可以参考之前的部分 链接在此 更改单个wi-fi连接顺序可以使用如下

  2. jquery mobile changepage的三种传参方法介绍

    本来觉得changePage 那么多option,传几个参数应该没问题结果翻遍国内外网站,基本方法只有三种 1,显性传参,就是利用url这个地址把参数带上,然后到changepage后的新页面,用函数 ...

  3. python文件操作指令

    原文地址:http://www.cnblogs.com/rollenholt/archive/2012/04/23/2466179.html 常用的文件操作指令: python中对文件.文件夹(文件操 ...

  4. CentOS7图文安装教程

    CentOS 7下载: CentOS 7只提供64位版本,虽然有不少国内镜像节点,不过还是觉得通过BT下载是不错的选择.镜像大小6.7G,联通20M光纤下载,不到小时.以下是中国大陆的下载地址列表: ...

  5. SSIS中出现数据流数据源假死状态的解决办法

    相信开发过Sql Server SSIS的人都遇到过在数据流中数据源假死的问题,特别是Excel Source特别容易假死,当job执行到数据流中的Excel Source时,既不报错也不执行,也没有 ...

  6. ubuntu 14.04 将窗体button移到右边

    刚刚安装了Ubuntu 14.04,想改动窗体button的位置.但依照曾经的办法发现不行了,在gconftool-->apps中找不到metacity. 多方查找后找到解决方式,例如以下 Ub ...

  7. Adapter(适配器)模式

    1. 概述: 接口的改变,是一个需要程序员们必须(虽然很不情愿)接受和处理的普遍问题.程序提供者们修改他们的代码;系统库被修正;各种程序语言以及相关库的发展和进化.  例子1:iphone4,你即可以 ...

  8. mysql将日期字符串转换

    举个例子: 给定字符串为07/31/2018,想要把格式转换成20180731 需要用到以下两个函数: date_format(date,’%Y-%m-%d’) ————–>oracle中的to ...

  9. nodejs运行的时候报错:Error: write EIO以及乱码解决方式

    在运行node.js的过程中报如下错误: events.js:72 throw er; // Unhandled 'error' event ^ Error: write EIO at errnoEx ...

  10. linux crontab 计划任务设置 (简结)

    命令: crontab  -l  查看当前运行的计划任务 crontab  -e  编辑当前运行计划任务 修改或添加 VIM编辑器用法:按 i 键进入编辑文本状态, esc 结束编辑状态 , :wq ...