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. IntelliJ IDEA 与Eclipse Link with Editor等价功能设置

    Link With Editor是Eclipse内置功能中十分小巧,但却异常实用的一个功能. 这个开关按钮 (Toggle Button) 出现在各式导航器视图 ( 例如 Resource Explo ...

  2. UVa 12661 - Funny Car Racing(Dijkstra)

    链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  3. 1968. [AHOI2005]约数研究【数论】

    Description Input 只有一行一个整数 N(0 < N < 1000000). Output 只有一行输出,为整数M,即f(1)到f(N)的累加和. Sample Input ...

  4. 2、RabbitMQ-simplest thing(简单队列)

    1.项目准备: 使用maven的依赖 <dependencies> <dependency> <groupId>com.rabbitmq</groupId&g ...

  5. docker-8-本地镜像发布到阿里云

    镜像的生成方法 1.前面的DockerFile   2.从容器创建一个新的镜像 docker commit [OPTIONS] 容器ID [REPOSITORY[:TAG]] 将本地镜像推送到阿里云 ...

  6. UnicodeEncodeError: 'ascii' codec can't encode characters in position 16-22: ordinal not in range(128)

    在python2.7下,将字符串写入到文件时会出现"UnicodeEncodeError: 'ascii' codec can't encode character u'\xa0' in p ...

  7. istringstream和ostringstream的实现

    ostringstream是将数据写入string里边的,istringstream是将从string里边读出数据的: #include <sstream> int main() { st ...

  8. 使用interface与类型诊断机制判断一个类型是否实现了某个方法

    Golang中的interface通常用来定义接口,在接口里提供一些方法,其他类型可以实现(implement)这些方法,通过将接口指针指向不同的类型实例实现多态(polymorphism),这是in ...

  9. 微服务—熔断器Hystrix

    前言在微服务架构中,我们将系统拆分成了一个个的服务单元,各单元应用间通过服务注册与发现的方式互相依赖. 由于每个单元都在不同的进程中运行,依赖通过远程调用的方式执行,这样就有可能因为网络原因或是依赖服 ...

  10. NSDate|NSTimeZone|时区|日历

    NSDate,NSDateFormatter以及时区转换-开发者-51CTO博客 iOS 时区转换 东八区 - 简书 iOS时间的时区转换以及一些方法记录 - 简书 iOS - OC NSTimeZo ...