__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 ...
随机推荐
- windows环境下 svn 客户端
- Linux分区介绍
分区的大小主要取决于个人的选择,以下内容可能会有一定帮助:/boot - 200 MB 实际需求大约 100 MB,如果有多个内核/启动镜像同时存在,建议分配 200 或者 300 MB./ - 15 ...
- WPF 中保存 window(窗口)或者canvas成图片
最近需要用到这个功能,搜了一下不少代码有问题 ,找到一个效果比较好的,支持多级子元素 记一下. private void button_save_window_Click(object sender, ...
- Storm简介
Storm特性 1. 低延迟和高性能 在一个小集群中,每个节点每秒可以处理数以百万计的消息. 2. 可扩展 在Storm集群中主要有三个实体:工作进程.线程和任务.Storm集群中每台机器上都可以运行 ...
- android四大组件之ContentProvider(一)
ContentProvider学习笔记 1. ContentProvider基本概念 ContentProvider向我们提供了我们在应用程序之间共享数据的一种机制,虽然采用文件和SharedPref ...
- Autocad 常用命令
一律使用的简写 1:将多条线段合并成一条多线段[pe] 2:如果发现合并的多线段将自己不想合并的区域合并进去了,别担心,炸开就行了 [x] 3:如果画的线段太长工作区不适应,使用缩放命令.[scale ...
- redis shell命令大全
redis shell命令大全(转自http://blog.mkfree.com/posts/5105432f975ad0eb7d135964) 作者:oyhk 2013-1-28 3:11:35 ...
- Drools给日志打标签
一.定义日志模型 public class Logger { // 日志编号 private String id; // 目标IP private String targetIp; // 目标端口 p ...
- 方法的重载(overload)和重写(override)的区别
方法的重写Overriding和重载Overloading是Java多态性的不同表现.重写Overriding是父类与子类之间多态性的一种表现,重载Overloading是一个类中多态性的一种表现.如 ...
- lua 中的面向对象
lua 是一种脚步语言,语言本身并不具备面向对象的特性. 但是我们依然可以利用语言的特性,模拟出面向对象的特性. 面向对象的特性通常会具备:封装,继承,多态的特性,如何在lua中实现这些特性,最主要的 ...