__new__
【__new__】
object.__new__(cls[, ...])
Called to create a new instance of class cls. 用于创建类对象cls的实例。
__new__() is a static method (special-cased so you need not declare it as such) __new__是一个特殊的方法,不需要声明为static方法。第一个参数是类对象。其余的参数会被传递给__init__。
If __new__() returns an instance of cls, then the new instance’s __init__() method will be invoked like __init__(self[, ...]), where self is the new instance and the remaining arguments are the same as were passed to __new__().
If __new__() does not return an instance of cls, then the new instance’s __init__() method will not be invoked.
__new__() is intended mainly to allow subclasses of immutable types (like int, str, or tuple) to customize instance creation. It is also commonly overridden in custom metaclasses in order to customize class creation.
__new__的更多相关文章
- __new__静态方法
__new__静态方法 新式类都有一个__new__的静态方法,它的原型是object.__new__(cls[, ...]) cls是一个类对象,当你调用C(*args, **kargs)来创建一个 ...
- __new__方法
__new__:创建对象时调用,返回当前对象的一个实例__init__:创建完对象后调用,对当前对象的实例的一些初始化,无返回值 案例一: >>> class A(object): ...
- Python中的__new__和__init__
Python中的__new__和__init__ 写了这么多的class,现在才知道还有个__new__方法, 那么它和__init__有什么区别呢? class TestCls(): "& ...
- Python中的__init__和__new__介绍
介绍 首先我们要知道在面向对象编程中,实例化基本遵循创建实例对象.初始化实例对象.最后返回实例对象这么一个过程. Python 中的 __new__ 方法负责创建一个实例对象,__init__ 方法负 ...
- [深入Python]__new__和__init__
class A(object): def __init__(self): print "init" def __new__(cls,*args, **kwargs): print ...
- __new__ 的简单应用
用__new__与__init__不同,通过继承内建类型对象,__new__可以用来创建一个简单的新类型,在__new__加入一些动作以完成创建. class RoundFloat(float): d ...
- python中的__init__ 、__new__、__call__小结
这篇文章主要介绍了python中的__init__ .__new__.__call__小结,需要的朋友可以参考下 1.__new__(cls, *args, **kwargs) 创建对象时调用,返回 ...
- [Python] Python 之 __new__() 方法与实例化
__new__() 是在新式类中新出现的方法,它作用在构造方法建造实例之前,可以这么理解,在 Python 中存在于类里面的构造方法 __init__() 负责将类的实例化,而在 __init__() ...
- Python中的__new__()方法的使用
__new__() 函数只能用于从object继承的新式类. 先看下object类中对__new__()方法的定义: class object: @staticmethod # known cas ...
随机推荐
- postfix启动脚本
使用该脚本是一定要注意postfix安装路径 #!/bin/bash # # postfix Postfix Mail Transger Agent # # chkconfig: # descript ...
- unique函数的作用
unique() 去重函数 unique()函数是一个去重函数,STL中unique的函数 unique的功能是去除相邻的重复元素(只保留一个),还有一个容易忽视的特性是它并不真正把重复的元素删除.他 ...
- Java引用总结--StrongReference、SoftReference、WeakReference、PhantomReference
Java引用总结--StrongReference.SoftReference.WeakReference.PhantomReference 1 Java引用介绍 Java从1.2版本开始引入了4种引 ...
- 清华申请退学博士作品:完全用Linux工作
http://www.cnblogs.com/cbscan/articles/3252872.html 下文地址 http://blog.oldboyedu.com/use-linux/ 按: 尽管我 ...
- maven 几个插件的使用
1. compile <plugin> <artifactId>maven-compiler-plugin</artifactId> <configurat ...
- 关于MySql的DBHelper类以及数据分页
前端: <%@ Register Assembly="AspNetPager" Namespace="Wuqi.Webdiyer" TagPrefix=& ...
- JSP+servlet简单登录实例
一个简单的jsp+servlet实例,实现简单的登录 转载▼ http://blog.sina.com.cn/s/blog_5c5bc9070100z7wb.html 开发环境myeclips ...
- Win32程序和控制台应用程序的项目互转设置
一般情况下,如果是windows程序,那么WinMain是入口函数,在VS2010中新建项目为"win32项目" 如果是dos控制台程序,那么main是入口函数,在VS2010中新 ...
- No.1__C#
这是第一篇C#的日记,到现在为止已经学习了一个礼拜的C#了.由于是实习中才开始学习,所以这次不准备像在大学学习那样,拿着课本划重点,背概念.这应当是一门实践的课程,应该一边编程,一边学.这是到公司第一 ...
- C++——CString用法大全
列表形式的如下: CString的构造函数CString( );例:CString csStr; CString( const CString& stringSrc );例:CString c ...