- 浏览: 240676 次
- 性别:
- 来自: 北京
文章分类
- 全部博客 (127)
- vim (3)
- python (44)
- pymysql (1)
- mysql (9)
- macvim (1)
- erlang (3)
- twisted (0)
- tornado (5)
- django (7)
- postgresql (5)
- sql (1)
- java (7)
- tech (4)
- cache (1)
- lifestyle (3)
- html (1)
- ubuntu (2)
- rabbitmq (1)
- algorithm (8)
- Linux (4)
- Pythonista (1)
- thread (1)
- sort (6)
- 设计模式 (1)
- search (1)
- Unix (6)
- Socket (3)
- C (2)
- web (1)
- gc (1)
- php (10)
- macos (1)
最新评论
-
2057:
这个程序有bug。
查找算法学习之二分查找(Python版本)——BinarySearch -
dotjar:
NB
一个Python程序员的进化[转]
Contains:
一、__init__通过此方法我们可以顶一个对象的初始操作。
但是当我新建一个类的实例的时候,__init__并不是第一个被调用的,实际上,还有一个叫做__new__的方法,来构造这个实例。然后在给开始创建的初始化函数传递参数。
二、__new__
__new__方法相当不常用,但是它有自己的特性,特别是当继承一个不可变的类型比如一个tuple或者string.
Q:可是什么时候使用__new__?
难道是这个吗?特别是当继承一个不可变的类型比如一个tuple或者string.
object.__new__(cls[, …])
Called to create a new instance of class cls. __new__() is a static method (special-cased so you need not declare it as such) that takes the class of which an instance was requested as its first argument. The remaining arguments are those passed to the object constructor expression (the call to the class). The return value of __new__() should be the new object instance (usually an instance of cls).
Typical implementations create a new instance of the class by invoking the superclass’s __new__() method using super(currentclass, cls).__new__(cls[, ...])with appropriate arguments and then modifying the newly-created instance as necessary before returning it
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.
三、__call__
这个方法可以让类的实例的行为表现的像函数一样,你可以调用他们,讲一个函数当做一个参数传到另外一个函数中等等。
result:
before __call__ (2, 3)
after __call__ (4, 5)
参考资料:
http://docs.python.org/2/reference/datamodel.html
http://stackoverflow.com/questions/8106900/new-and-init-in-python
http://stackoverflow.com/questions/674304/pythons-use-of-new-and-init
http://www.wingide.com/psupport/python-manual/2.4/ref/customization.html
http://stackoverflow.com/questions/12971641/need-to-understand-the-flow-of-init-new-and-call?lq=1
http://pycoders-weekly-chinese.readthedocs.org/en/latest/issue6/a-guide-to-pythons-magic-methods.html
- __new__: 创建对象时调用,返回当前对象的一个实例
- __init__:创建完成对象后调用,对当前对象的实例的一些初始化,无返回值
- __call__:
一、__init__通过此方法我们可以顶一个对象的初始操作。
但是当我新建一个类的实例的时候,__init__并不是第一个被调用的,实际上,还有一个叫做__new__的方法,来构造这个实例。然后在给开始创建的初始化函数传递参数。
二、__new__
@staticmethod # known case of __new__ def __new__(cls, *more): # known special case of object.__new__ """ T.__new__(S, ...) -> a new object with type S, a subtype of T """ pass
__new__方法相当不常用,但是它有自己的特性,特别是当继承一个不可变的类型比如一个tuple或者string.
Q:可是什么时候使用__new__?
难道是这个吗?特别是当继承一个不可变的类型比如一个tuple或者string.
引用
object.__new__(cls[, …])
Called to create a new instance of class cls. __new__() is a static method (special-cased so you need not declare it as such) that takes the class of which an instance was requested as its first argument. The remaining arguments are those passed to the object constructor expression (the call to the class). The return value of __new__() should be the new object instance (usually an instance of cls).
Typical implementations create a new instance of the class by invoking the superclass’s __new__() method using super(currentclass, cls).__new__(cls[, ...])with appropriate arguments and then modifying the newly-created instance as necessary before returning it
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.
三、__call__
引用
Called when the instance is “called” as a function; if this method is defined, x(arg1, arg2, ...) is a shorthand for x.__call__(arg1, arg2, ...).
这个方法可以让类的实例的行为表现的像函数一样,你可以调用他们,讲一个函数当做一个参数传到另外一个函数中等等。
class Entity: def __init__(self,x,y): self.x = x self.y = y def __call__(self, x, y): self.x = x self.y = y entity = Entity(2,3) print 'before __call__',(entity.x,entity.y) entity.__call__(4,5) print 'after __call__',(entity.x,entity.y)
result:
before __call__ (2, 3)
after __call__ (4, 5)
参考资料:
http://docs.python.org/2/reference/datamodel.html
http://stackoverflow.com/questions/8106900/new-and-init-in-python
http://stackoverflow.com/questions/674304/pythons-use-of-new-and-init
http://www.wingide.com/psupport/python-manual/2.4/ref/customization.html
http://stackoverflow.com/questions/12971641/need-to-understand-the-flow-of-init-new-and-call?lq=1
http://pycoders-weekly-chinese.readthedocs.org/en/latest/issue6/a-guide-to-pythons-magic-methods.html
发表评论
-
macos 10.9.2 clang: error: unknown argument: '-mno-fused-madd' [-Wunused-command
2014-03-25 19:13 1716方法总是有的,当然需要你去寻找。 当然如果花费太多的时间在一件 ... -
PostgreSQL psycopg2:IndexError: tuple index out of range
2014-01-09 17:04 2178Postgresql psycopg2使用like查询的时候 ... -
Python 迭代器和生成器
2013-10-15 23:09 2797迭代器 迭代器只不过是一个实现迭代器协议的容器对象。它基于两个 ... -
Python时间模块
2013-10-15 23:03 3417time模块 时间模块中最常用的一个函数就是获取当前时间的函数 ... -
Python装饰器
2013-10-15 22:59 1497编写自定义装饰器有许多方法,但最简单和最容易理解的方法是编写一 ... -
python list
2013-10-15 22:56 1203简单总结以及整理如下: >>> dir( ... -
Python Excel
2013-09-10 17:21 924安装lib easy_install xlrd def ... -
排序算法学习(python版本)之堆排序(HeapSort)
2013-07-01 22:54 1951Contains: 堆排序以及堆排序的应用 堆排序(Heaps ... -
python range xrange
2013-06-25 23:30 1096引用Help on built-in function ran ... -
python class
2013-06-25 00:54 1780引用类是创建新对象类 ... -
AttributeError: 'module' object has no attribute 'SendCloud'
2013-06-05 11:46 7010网上查了下 意思是说你命名的文件名不能和lib重名,这样会导 ... -
python string
2013-05-07 23:44 2154如果这就是字符串,这本来就是字符串 首先看下字符串的方法 ... -
Python property
2013-03-29 19:56 0由于之前有总结过,可以参考http://2057.iteye. ... -
python tips
2013-03-28 23:57 8421、enum #!/usr/bin/env python ... -
python decorators
2013-03-28 23:36 1316Contains: 1、decorators 2、funct ... -
python closures
2013-03-28 22:09 1140Closure:如果在一个内部函数里,对在外部作用域(但不是在 ... -
Python map、filter,reduce介绍
2013-03-28 22:02 12261、filter(function,iterable) 引用C ... -
Python socket简介
2013-03-25 23:42 2108自豪地使用dir和help. Python 2.7.2 ( ... -
Tornado ioloop源码简析
2013-03-21 00:18 2791#!/usr/bin/env python #-*-en ... -
Tornado httpserver 源码简析
2013-03-17 01:49 1728整个流程就是创建一个socket socket.socket ...
相关推荐
主要给大家介绍了关于python中__init__、__new__和__call__方法的相关资料,文中通过示例代码介绍的非常详细,对大家具有一定的参考学习价值,需要的朋友可以参考学习,下面来跟着小编一起看看吧。
__call__ : 对象可call,注意不是类,是对象。 先有创建,才有初始化。即先__new__,而后__init__。 上面说的不好理解,看例子。 1.对于__new__ class Bar(object): pass class Foo(object): def __new__(cls, ...
注意,这里的第一个参数是self即对象本身【注意和new的区别】3.__call__(self, *args, **kwargs) 如果类实现了这个方法,相当于把这个类型的对象当作函数来使用,相当于 重载了括号运算符 看具体的例子:复制代码...
What’s New in Python What’s New In Python 3.6 Summary – Release highlights New Features PEP 498: Formatted string literals PEP 526: Syntax for variable annotations PEP 515: Underscores in ...
Python Cookbook, 2nd Edition By David Ascher, Alex Martelli, Anna Ravenscroft Publisher : O'Reilly Pub Date : March 2005 ISBN : 0-596-00797-3 Pages : 844 Copyright Preface The ...
复制代码 代码如下:C.__init__(self[, arg1, …]) 构造器(带一些可选的参数)C.__new__(self[, arg1, …]) 构造器(带一些可选的参数);通常用在设置不变数据类型的子类。C.__del__(self) 解构器C.__str__(self) ...
5.3 Calling a Superclass _ _init_ _ Method if It Exists 5.4 Calling a Superclass Implementation of a Method 5.5 Implementing Properties 5.6 Implementing Static Methods 5.7 Implementing Class ...
call_function()函数有两个重载版本.一个是根据函数的名字来调用函数, 另一个是调用一个可以作为函数调用的Lua值. 使用函数名来调用的版本只能调用Lua全局函数. "..."代表传递给Lua函数的 可变个数的参数. 这使得你...
In particular, do not add new parameters to the end of the function just because they are new; place new input-only parameters before the output parameters. This is not a hard-and-fast rule. ...
Smaliemu 这是的PY3版本。 安装 pip install smaliemu 用法 from smaliemu.emulator import Emulator emu = Emulator() snippet = [ ...-><init>([B)V', 'return-object v0' ] ret = emu.call(snippet) pri
图像节点 用于与 Imgur API 交互的节点包。 这是官方 Python Imgur API imgurpython 的一个端口。 见: : ... // Call init to validate access token // Will refresh access token if refresh to