Python __init__ __new__ 有和区别

xiehongan123 2010-05-10 09:22:44
如题
另外,这两个方法的有什么作用
在Python里面有很多__开头的方法,他们一般是些什么方法?
Python的面向对象我还像有或多的困惑,怎么样才能理解清楚?能否有人传授点秘诀或是给个链接?
...全文
4868 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
国谦哥哥 2012-09-25
  • 打赏
  • 举报
回复
学习了。多谢。
wahaha00239 2011-08-15
  • 打赏
  • 举报
回复
3Q,有了一份理解了
firmlyjin 2011-01-23
  • 打赏
  • 举报
回复
very good
xiehongan123 2010-05-25
  • 打赏
  • 举报
回复
谢谢大家,
龙根 2010-05-19
  • 打赏
  • 举报
回复
不懂,顶下
ultimatebuster 2010-05-18
  • 打赏
  • 举报
回复
其实所有的python function都返回NONE,除非你特殊指出return
notax 2010-05-18
  • 打赏
  • 举报
回复
楼上说的没错,先谢谢啦


如果 __new__ 有return instance 的话,应该是等於call 了__init__

http://docs.python.org/reference/datamodel.html#object.__new__



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.

amu9900 2010-05-18
  • 打赏
  • 举报
回复
__new__:创建对象时调用,返回当前对象的一个实例相当于java里面的构造器差不多

__init__:创建完对象后调用,对当前对象的实例的一些初始化,无返回值

如果重写了__new__而在__new__里面没有调用__init__那么__init__将不起作用。

可以这样理解,默认是创建(__new__),然后调用__init__,所以一楼的例子举错了。
vanta 2010-05-17
  • 打赏
  • 举报
回复
我一般都是用__init__,在python里面似乎很少要用到__new__,
tomsheep 2010-05-15
  • 打赏
  • 举报
回复
mark
四哥 2010-05-14
  • 打赏
  • 举报
回复
__new__一般是用于继承内置类的,返回值是一个对象,__init__只做一些初始化工作,没有返回值
jamseyang 2010-05-14
  • 打赏
  • 举报
回复
学习下。。。
zfzaizheli 2010-05-13
  • 打赏
  • 举报
回复
不懂不懂,了解中。。。
xs810559195 2010-05-13
  • 打赏
  • 举报
回复
__开头的方法是私有方法,别的类用不了
一土草三工 2010-05-11
  • 打赏
  • 举报
回复
mark xuexi
Semigod 2010-05-11
  • 打赏
  • 举报
回复
很好,学习了一下
notax 2010-05-10
  • 打赏
  • 举报
回复
一般很少情框下用到__new__,
__new__ 比 __init__ 优先


class A(object):
def __init__(self):
print ("__init__")

def __new__(self):
print ("__new__")


A()

$ python test.py
__new__




http://www.python.org/download/releases/2.2/descrintro/



When subclassing immutable built-in types like numbers and strings, and occasionally in other situations, the static method __new__ comes in handy. __new__ is the first step in instance construction, invoked before __init__. The __new__ method is called with the class as its first argument; its responsibility is to return a new instance of that class. Compare this to __init__: __init__ is called with an instance as its first argument, and it doesn't return anything; its responsibility is to initialize the instance. There are situations where a new instance is created without calling __init__ (for example when the instance is loaded from a pickle). There is no way to create a new instance without calling __new__ (although in some cases you can get away with calling a base class's __new__).

Recall that you create class instances by calling the class. When the class is a new-style class, the following happens when it is called. First, the class's __new__ method is called, passing the class itself as first argument, followed by any (positional as well as keyword) arguments received by the original call. This returns a new instance. Then that instance's __init__ method is called to further initialize it. (This is all controlled by the __call__ method of the metaclass, by the way.)

Here is an example of a subclass that overrides __new__ - this is how you would normally use it.

>>> class inch(float):
... "Convert from inch to meter"
... def __new__(cls, arg=0.0):
... return float.__new__(cls, arg*0.0254)
...
>>> print inch(12)
0.3048
>>>

This class isn't very useful (it's not even the right way to go about unit conversions) but it shows how to extend the constructor of an immutable type. If instead of __new__ we had tried to override __init__, it wouldn't have worked:

>>> class inch(float):
... "THIS DOESN'T WORK!!!"
... def __init__(self, arg=0.0):
... float.__init__(self, arg*0.0254)
...
>>> print inch(12)
12.0
>>>

The version overriding __init__ doesn't work because the float type's __init__ is a no-op: it returns immediately, ignoring its arguments.

All this is done so that immutable types can preserve their immutability while allowing subclassing. If the value of a float object were initialized by its __init__ method, you could change the value of an existing float object! For example, this would work:

>>> # THIS DOESN'T WORK!!!
>>> import math
>>> math.pi.__init__(3.0)
>>> print math.pi
3.0
>>>

37,744

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • WuKongSecurity@BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧