标签:singleton int span str pytho 魔术 trace cal turn
对象.属性
对象.方法()
类.属性
类.方法()
class MyClass(): abc = 123 def __new__(cls): #把class这个类传递__new__这个方法 print (123) return None #返回一个None #实例化对象 obj = MyClass() print (obj)
执行
这样就不能使用使用obj这个对象调用abc的值
class MyClass(): abc = 123 def __new__(cls): print (123) return None #实例化对象 obj = MyClass() print (obj) print (obj.abc) #在这里就相当于使用None.abc.这种此写法不允许,就会报错
执行
借助父类创建对象
返回本类对象
class MyClass(): abc = 123 def __new__(cls):
print (cls) print (123) #要借助父类object的__new__创建对象 obj = object.__new__(cls) return obj #实例化对象 obj = MyClass() print (obj) print (obj.abc)
执行
[root@node10 python]# python3 test.py
<class ‘__main__.MyClass‘> #cls是一个类 123 <__main__.MyClass object at 0x7ff2808ba3c8> 123 [root
返回空对象
class MyClass(): abc = 123 def __new__(cls): print (cls) print (123) #要借助父类object的__new__创建对象 obj = object.__new__(cls) #return obj return None #实例化对象 obj = MyClass() print (obj) print (obj.abc)
执行
返回一个其他类的对象
class MyClass2(): ccc = 4 obj2 = MyClass2() class MyClass(): abc = 123 def __new__(cls): print (cls) print (123) #要借助父类object的__new__创建对象 obj = object.__new__(cls) #return obj #return None return obj2 #实例化对象 obj = MyClass() print (obj) print (obj.abc)
执行
修改属性
[root@node10 python]# cat test.py class MyClass2(): ccc = 4 obj2 = MyClass2() class MyClass(): abc = 123 def __new__(cls): print (cls) print (123) #要借助父类object的__new__创建对象 obj = object.__new__(cls) #return obj #return None return obj2 #实例化对象 obj = MyClass() print (obj) print (obj.ccc)
执行
[root@node10 python]# python3 test.py <class ‘__main__.MyClass‘> 123 <__main__.MyClass2 object at 0x7fb463cc7470> 4
调用的顺序
class Boat(): def __new__(cls): print (1) return object.__new__(cls) def __init__(self): print (2) obj = Boat() print (obj)
执行
[root@node10 python]# python3 test.py 1 2 <__main__.Boat object at 0x7f9bb5fcf400>
先调用__new__在调用__init__
调用和定义的顺序无关
class Boat(): def __init__(self): print (2) def __new__(cls): print (1) return object.__new__(cls) obj = Boat() print (obj)
执行
[root@node10 python]# python3 test.py 1 2 <__main__.Boat object at 0x7f95a73cf400>
结果一样
__new__ 的触发时机要快于 __init__ __new__ 是用来创建对象的 __init__ 是用来初始化对象的 先得有对象 才能初始化对象 new 和 init 他门的参数要保持一致.
class Boat(): def __new__(cls): print (1) #return object.__new__(cls) return None def __init__(self): print (2) obj = Boat() print (obj)
执行
[root@node10 python]# python3 test.py 1 None
初始化参数
class Boat(): def __new__(cls): print (1) return object.__new__(cls) #return None def __init__(self,name): self.name = name obj = Boat("John") print (obj.name)
执行
[root@node10 python]# python3 test.py Traceback (most recent call last): File "test.py", line 8, in <module> obj = Boat("John") TypeError: __new__() takes 1 positional argument but 2 were given
这是因为,__new__先于__init__调用,但是,在调用的时候,已经传递了一个类(cls)的参数,name的参数就不能对应报错
class Boat(): def __new__(cls,name): print (1) return object.__new__(cls) #return None def __init__(self,name): self.name = name obj = Boat("John") print (obj.name)
执行,而可以调用
[root@node10 python]# python3 test.py 1 John
lass Boat(): def __new__(cls,*args,**kwargs): print (1) return object.__new__(cls) #return None def __init__(self,*args,**kwargs): strvar = "" for i in args: strvar += i + " " print (strvar) print (kwargs) obj = Boat("John","Jim","Tom",name="David")
执行
[root@node10 python]# python3 test.py 1 John Jim Tom {‘name‘: ‘David‘}
class MyClass2(): ccc = 4 obj2 = MyClass2() class Boat(): def __new__(cls,*args,**kwargs): print (1) #return object.__new__(cls) return obj2 def __init__(self,*args,**kwargs): print (111) obj = Boat("John","Jim","Tom",name="David") print (obj.ccc)
执行
[root@node10 python]# python3 test.py 1 4
无论实例化类几次,都有且只有一个对象.为了节省内存空间
[root@node10 python]# cat test.py class Singleton(): pass obj1 = Singleton() print (obj1) obj2 = Singleton() print (obj2) obj3 = Singleton() print (obj3)
执行
[root@node10 python]# python3 test.py <__main__.Singleton object at 0x7efd7ec76400> <__main__.Singleton object at 0x7efd7ec76438> <__main__.Singleton object at 0x7efd7ec76470>
可以看到占用不同的内存空间
class Singleton(): #创建一个私有的对象,类外无法调用 __obj = None #定义一个方法 def __new__(cls): #判断这个对象是否为None对象 if cls.__obj is None: obj = object.__new__(cls) #如果是,就重新创建一个新的对象 cls.__obj = obj #把这新的对象夫给类的私有方法 return cls.__obj #如果不是None,直接返回 obj1 = Singleton() print (obj1) obj2 = Singleton() print (obj2) obj3 = Singleton() print (obj3)
执行
[root@node10 python]# python3 test.py <__main__.Singleton object at 0x7fbae7edb4a8> <__main__.Singleton object at 0x7fbae7edb4a8> <__main__.Singleton object at 0x7fbae7edb4a8>
发现三个对象都是一个内存地址
class Singleton(): #创建一个私有的对象,类外无法调用 __obj = None #定义一个方法 def __new__(cls,name): #判断这个对象是否为None对象 if cls.__obj is None: cls.__obj = object.__new__(cls) return cls.__obj def __init__(self,name): self.name = name obj1 = Singleton("John") obj2 = Singleton("Frnak") print (obj1.name) print (obj2.name)
执行
[root@node10 python]# python3 test.py Frnak Frnak
标签:singleton int span str pytho 魔术 trace cal turn
原文地址:https://www.cnblogs.com/zyxnhr/p/12344977.html