python 创建实例--待完善
今天好好琢磨一下 python 创建实例的先后顺序
一、 就定义一个普通类 Util (默认)继承自 object,覆写 new ,init 方法
class Util(object):
def __new__(cls,*args,**kw):
print('-----Util----__new__ ----start---')
print('cls: {}'.format(cls))
print('args: {}'.format(args))
{print('kw:',key,'<--->',value,'\n') for key,value in kw.items()}
return object.__new__(cls)
def __init__(self,*args,**kw):
print('-----Util----__init__ ----start---')
print('self: {}'.format(self))
print('args: {}'.format(args))
{print(key,'<--->',value,'\n') for key,value in kw.items()}
return super(Util,self).__init__()
args =(1,2,3)
kw = dict(name='frank',city='changsha')
util = Util(*args,**kw)
print(util)
输出结果:
-----Util----__new__ ----start---
cls: <class '__main__.Util'>
args: (1, 2, 3)
kw: name <---> frank
kw: city <---> changsha
-----Util----__init__ ----start---
self: <__main__.Util object at 0x7f4d54082a90>
args: (1, 2, 3)
name <---> frank
city <---> changsha
<class '__main__.Util'>
由上面可以知道 new 优先于 init 执行,如果 __new__
中没有 return 语句,则不会执行object 的 new 方法,而 init 在 object 中是在 new 中调用的,所以,此刻如下图, Util 中的 init 并不会被调用,只是调用了 Util 类的 new 方法,打印type(util) 得到的是 类类型 --》 NoneType ! 因为 构造方法init
没有被调用,也能理解还没有变为对象啊!!!
class Util(object):
def __new__(cls,*args,**kw):
print('-----Util----__new__ ----start---')
print('cls: {}'.format(cls))
print('args: {}'.format(args))
{print('kw:',key,'<--->',value,'\n') for key,value in kw.items()}
# return object.__new__(cls)
def __init__(self,*args,**kw):
print('-----Util----__init__ ----start---')
print('self: {}'.format(self))
print('args: {}'.format(args))
{print(key,'<--->',value,'\n') for key,value in kw.items()}
return super(Util,self).__init__()
args =(1,2,3)
kw = dict(name='frank',city='changsha')
util = Util(*args,**kw)
print(type(util))
结果:
-----Util----__new__ ----start---
cls: <class '__main__.Util'>
args: (1, 2, 3)
kw: name <---> frank
kw: city <---> changsha
<class 'NoneType'>
接下来,我们给Util 添加元类,由下图可以看出元类优先于Util 所有方法执行
class UtilMetaclass(type):
def __new__(meta_cls,cls,bases,attr_dict):
print('------UtilMetaclass---__new__ ---start----')
print('meta_cls: {}'.format(meta_cls))
print('cls: {}'.format(cls))
print('bases:{}'.format(bases))
print('attr_dict: {}\n'.format(attr_dict))
return type.__new__(meta_cls,cls,bases,attr_dict)
# def __init__(self,*args,**kw):
# print('-----UtilMetaclass----__init__ ----start---')
# print('self: {}'.format(self))
# print('args: {}\n'.format(args))
# {print('kw:',key,'<--->',value,'\n') for key,value in kw.items()}
# return super(UtilMetaclass,self).__init__(*args,**kw)
class Util(object,metaclass=UtilMetaclass):
def __new__(cls,*args,**kw):
print('-----Util----__new__ ----start---')
print('cls: {}'.format(cls))
print('args: {}'.format(args))
{print('kw:',key,'<--->',value,'\n') for key,value in kw.items()}
return object.__new__(cls)
def __init__(self,*args,**kw):
print('-----Util----__init__ ----start---')
print('self: {}'.format(self))
print('args: {}'.format(args))
{print(key,'<--->',value,'\n') for key,value in kw.items()}
return super(Util,self).__init__()
args =(1,2,3)
kw = dict(name='frank',city='changsha')
util = Util(*args,**kw)
print(type(util))
输出结果:
------UtilMetaclass---__new__ ---start----
meta_cls: <class '__main__.UtilMetaclass'>
cls: Util
bases:(<class 'object'>,)
attr_dict: {'__module__': '__main__', '__qualname__': 'Util', '__init__': <function Util.__init__ at 0x7f4d540e99d8>, '__new__': <function Util.__new__ at 0x7f4d540e9ea0>}
-----Util----__new__ ----start---
cls: <class '__main__.Util'>
args: (1, 2, 3)
kw: name <---> frank
kw: city <---> changsha
-----Util----__init__ ----start---
self: <__main__.Util object at 0x7f4d5409cb70>
args: (1, 2, 3)
name <---> frank
city <---> changsha
<class '__main__.Util'>
最后我们来看看给 元类 覆写掉其父类 type 的构造方法,却注释掉return super 语句
class UtilMetaclass(type):
def __new__(meta_cls,cls,bases,attr_dict):
print('------UtilMetaclass---__new__ ---start----')
print('meta_cls: {}'.format(meta_cls))
print('cls: {}'.format(cls))
print('bases:{}'.format(bases))
print('attr_dict: {}\n'.format(attr_dict))
return type.__new__(meta_cls,cls,bases,attr_dict)
def __init__(self,*args,**kw):
print('-----UtilMetaclass----__init__ ----start---')
print('self: {}'.format(self))
print('args: {}\n'.format(args))
{print('kw:',key,'<--->',value,'\n') for key,value in kw.items()}
#return super(UtilMetaclass,self).__init__(*args,**kw)
class Util(object,metaclass=UtilMetaclass):
def __new__(cls,*args,**kw):
print('-----Util----__new__ ----start---')
print('cls: {}'.format(cls))
print('args: {}'.format(args))
{print('kw:',key,'<--->',value,'\n') for key,value in kw.items()}
return object.__new__(cls)
def __init__(self,*args,**kw):
print('-----Util----__init__ ----start---')
print('self: {}'.format(self))
print('args: {}'.format(args))
{print(key,'<--->',value,'\n') for key,value in kw.items()}
return super(Util,self).__init__()
args =(1,2,3)
kw = dict(name='frank',city='changsha')
util = Util(*args,**kw)
print(type(util))
输出结果:
------UtilMetaclass---__new__ ---start----
meta_cls: <class '__main__.UtilMetaclass'>
cls: Util
bases:(<class 'object'>,)
attr_dict: {'__module__': '__main__', '__qualname__': 'Util', '__init__': <function Util.__init__ at 0x7f4d542b2ea0>, '__new__': <function Util.__new__ at 0x7f4d542b2488>}
-----UtilMetaclass----__init__ ----start---
self: <class '__main__.Util'>
args: ('Util', (<class 'object'>,), {'__module__': '__main__', '__qualname__': 'Util', '__init__': <function Util.__init__ at 0x7f4d542b2ea0>, '__new__': <function Util.__new__ at 0x7f4d542b2488>})
-----Util----__new__ ----start---
cls: <class '__main__.Util'>
args: (1, 2, 3)
kw: name <---> frank
kw: city <---> changsha
-----Util----__init__ ----start---
self: <__main__.Util object at 0x7f4d540f4a20>
args: (1, 2, 3)
name <---> frank
city <---> changsha
<class '__main__.Util'>
添加上这个 return 语句 其实发现也没什么变化,这就从侧面说明了 new 会调用 init ,而 init 会到继承链(我取的名字)上去找。。。添加上 return 不过是再次调用了 type 的 构造方法罢了,其实没有必要,一般 init 方法中是不需要返回值的这点跟java一样。。。
class UtilMetaclass(type):
def __new__(meta_cls,cls,bases,attr_dict):
print('------UtilMetaclass---__new__ ---start----')
print('meta_cls: {}'.format(meta_cls))
print('cls: {}'.format(cls))
print('bases:{}'.format(bases))
print('attr_dict: {}\n'.format(attr_dict))
return type.__new__(meta_cls,cls,bases,attr_dict)
def __init__(self,*args,**kw):
print('-----UtilMetaclass----__init__ ----start---')
print('self: {}'.format(self))
print('args: {}\n'.format(args))
{print('kw:',key,'<--->',value,'\n') for key,value in kw.items()}
return super(UtilMetaclass,self).__init__(*args,**kw)
class Util(object,metaclass=UtilMetaclass):
def __new__(cls,*args,**kw):
print('-----Util----__new__ ----start---')
print('cls: {}'.format(cls))
print('args: {}'.format(args))
{print('kw:',key,'<--->',value,'\n') for key,value in kw.items()}
return object.__new__(cls)
def __init__(self,*args,**kw):
print('-----Util----__init__ ----start---')
print('self: {}'.format(self))
print('args: {}'.format(args))
{print(key,'<--->',value,'\n') for key,value in kw.items()}
return super(Util,self).__init__()
args =(1,2,3)
kw = dict(name='frank',city='changsha')
util = Util(*args,**kw)
print(type(util))
输出结果:
------UtilMetaclass---__new__ ---start----
meta_cls: <class '__main__.UtilMetaclass'>
cls: Util
bases:(<class 'object'>,)
attr_dict: {'__module__': '__main__', '__qualname__': 'Util', '__init__': <function Util.__init__ at 0x7f4d542b2d90>, '__new__': <function Util.__new__ at 0x7f4d542b29d8>}
-----UtilMetaclass----__init__ ----start---
self: <class '__main__.Util'>
args: ('Util', (<class 'object'>,), {'__module__': '__main__', '__qualname__': 'Util', '__init__': <function Util.__init__ at 0x7f4d542b2d90>, '__new__': <function Util.__new__ at 0x7f4d542b29d8>})
-----Util----__new__ ----start---
cls: <class '__main__.Util'>
args: (1, 2, 3)
kw: name <---> frank
kw: city <---> changsha
-----Util----__init__ ----start---
self: <__main__.Util object at 0x7f4d540f4518>
args: (1, 2, 3)
name <---> frank
city <---> changsha
<class '__main__.Util'>
class SingletonMetaclass(type):
def __new__(cls,*args,**kw):
print('new in SingletonMetaclass start...')
return super(SingletonMetaclass,cls).__new__(cls,*args,**kw)
def __init__(self,*args,**kw):
print('init in SingletonMetaclass start...')
def __call__(cls,*args,**kw):
print('call in SingletonMetaclass start...')
if not hasattr(cls,'obj'):
cls.obj = cls.__new__(cls,*args,**kw)
cls.__init__(cls.obj,*args,**kw)
return cls.obj
class Singleton(object,metaclass=SingletonMetaclass):
def __new__(cls,*args,**kw):
print('new in Singleton start...')
return super(Singleton,cls).__new__(cls,*args,**kw)
def __init__(self,*args,**kw):
print('init in Singleton start...')
def __call__(self,*args,**kw):
print('call in Singleton start...')
Singleton
s1 = Singleton()
s2 = Singleton()
s1 == s2
python 创建实例--待完善的更多相关文章
- python 创建实例对象
实例化类其他编程语言中一般用关键字 new,但是在 Python 中并没有这个关键字,类的实例化类似函数调用方式. 以下使用类的名称 Employee 来实例化,并通过 __init__ 方法接收参数 ...
- openstack私有云布署实践【19 通过python客户端 创建实例VM指定IP地址】
还有一种创建方式 是使用py开发工具,调用openstackclient的方法进行创建实例 ,好处就是可随意指定我们要的虚拟机IP地址,需求的场景就是,某天我们需要主动分配一个比较熟知的IP用作某个服 ...
- python之定义类创建实例
https://www.cnblogs.com/evablogs/p/6688938.html 类的定义 在Python中,类通过class关键字定义,类名以大写字母开头 1 2 >>&g ...
- python创建MySQL多实例-1
python创建MySQL多实例-1 前言 什么是多实例 多实例就是允许在同一台机器上创建另外一套不同配置文件的数据库,他们之间是相互独立的,主要有以下特点, 1> 不能同时使用一个端口 2&g ...
- python基础教程:定义类创建实例
类的定义 在Python中,类通过class关键字定义,类名以大写字母开头 >>>class Person(object): #所有的类都是从object类继承 pass #pass ...
- python基础——实例属性和类属性
python基础——实例属性和类属性 由于Python是动态语言,根据类创建的实例可以任意绑定属性. 给实例绑定属性的方法是通过实例变量,或者通过self变量: class Student(objec ...
- 1.面向过程编程 2.面向对象编程 3.类和对象 4.python 创建类和对象 如何使用对象 5.属性的查找顺序 6.初始化函数 7.绑定方法 与非绑定方法
1.面向过程编程 面向过程:一种编程思想在编写代码时 要时刻想着过程这个两个字过程指的是什么? 解决问题的步骤 流程,即第一步干什么 第二步干什么,其目的是将一个复杂的问题,拆分为若干的小的问题,按照 ...
- Python 创建和使用类
python创建和使用类的方法如下 # class Dog(): # def __init__(self,name,age): # self.name=name # self.age=age # # ...
- 使用python创建mxnet操作符(网络层)
对cuda了解不多,所以使用python创建新的操作层是个不错的选择,当然这个性能不如cuda编写的代码. 在MXNET源码的example/numpy-ops/下有官方提供的使用python编写新操 ...
随机推荐
- mysql distinct 去重
在使用MySQL时,有时需要查询出某个字段不重复的记录,这时可以使用mysql提供的distinct这个关键字来过滤重复的记录,但是实际中我们往往用distinct来返回不重复字段的条数(count( ...
- win8和win7下解决php5.3和5.4、5.5等不能加载php_curl.dll的终极解决办法 收藏
win8和win7下解决php5.3和5.4.5.5等不能加载php_curl.dll的终极解决办法 收藏2015年01月11日 最近分别在WIN7和Windows8 上分别安装php 高版本!都遇到 ...
- [Wiki].NET框架
.NET框架 建议将.NET Framework 3.0并入本条目或章节.(讨论) .NET框架 .NET框架的组件堆栈 开发者 Microsoft 初始版本 2002年2月13日,16年前 稳定 ...
- Jquery 组 checkbox全选checkbox
<!DOCTYPE html><html lang="zh-cn"><head> <meta charset="utf-8&qu ...
- JIRA & GitHub
JIRA & GitHub https://confluence.atlassian.com/adminjiracloud/connect-jira-cloud-to-github-81418 ...
- delphi 的 LockType 锁类型
LockType 锁类型 常数 值 说明 ...
- codeforces548B
Mike and Fun CodeForces - 548B Mike and some bears are playing a game just for fun. Mike is the judg ...
- std::binary_serach, std::upper_bound以及std::lower_bound
c++二分查找的用法 主要是 std::binary_serach, std::upper_bound以及std::lower_bound 的用法,示例如下: std::vector<int& ...
- Luogu4782 【模板】2-SAT 问题(2-SAT)
模板.注意若x=y不一定是废话,x=0或x=0表示x必须为0.以及数组开2n. #include<iostream> #include<cstdio> #include< ...
- Scout YYF I POJ - 3744(概率dp + 矩阵快速幂)
题意: 一条路上有n个地雷,你从1开始走,单位时间内有p的概率走一步,1-p的概率走两步,问安全通过这条路的概率 解析: 很容易想到 dp[i] = p * dp[i-1] + (1 - p) * d ...