__init __()不会自动调用 [英] __init__() not called automatically

查看:130
本文介绍了__init __()不会自动调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我来自c ++背景。我很高兴发现自己非常熟悉Python。但是,让我感到惊讶的是__init __(),这被认为是

c ++中构造函数的等价物,并不是自动调用的。我确定必须有足够的理由

。我想知道为什么会这样?这是我对程序员的负担更多



同样,为什么我们必须明确使用''self''关键字

每次?


欢迎各种帮助。

解决方案

25 2005年5月21:31:57 -0700,Sriek< sc ******* @ gmail.com>写道:


我来自c ++背景。我很高兴发现自己非常熟悉Python。但是,让我感到惊讶的是__init __(),它被认为是c ++中构造函数的等价物,并不是自动调用的。我相信这一定有充足的理由。我想知道为什么会这样?这是我对程序员的负担更多的负担。

C类:
.... def __init __(self):print" Hello"

.... c = C()



你好


这看起来像__init__被自动调用给我。你是否在做一些与众不同的事情?

同样,为什么我们每次都必须明确地使用''self''关键字?

http://www.python.org/doc/faq/genera...ions-and-calls

每一个我们欢迎各种帮助。


不用担心,


Tim

-
http://mail.python.org/mailman/listinfo/python-list



Sriek写道:


我来自c ++背景。我很高兴发现自己非常熟悉Python。但是,让我感到惊讶的是__init __(),它被认为是c ++中构造函数的等价物,并不是自动调用的。


你是什么意思? :


Python 2.4.1(#2,2005年5月5日,09:45:41)

[GCC 4.0.0 20050413(预发布)(Debian) linux2上的4.0-0pre11)]

输入help,copyright,credit等等。或许可证或更多信息。

A类(对象):
.... def __init __(self):

.... print in in __init __"

.... a = A()



in __init__


所以__init__肯定会在实例化时被调用。确实如果

你从A派生并覆盖__init__,A .__ init__不会被称为

除非明确地这样做:


B级(A):

def __init __(自我):

print" in B .__ init __()"

super(B,self).__ init __()

我确定必须有足够的理由这个。我想知道为什么会这样?这是我对程序员的负担更多的负担。


这不是那么多实际的负担,IMO它是完全合理的。

当你覆盖一个类的方法时,你想拥有明确

调用超类代码,不要让它自动运行,否则你输了

控制流量。


同样,为什么我们每次都必须明确使用''self''关键字?


这更接近疣,IMO,但是一旦你使用了Python一段时间

你会明白为什么这是所以。基本上,在
Python中的所有内容都是名称空间或命名空间中的名称。在

的情况下,Python自动发送的自引用作为第一个arg自动引用,

方法需要将其绑定到本地名称,按照约定

only,''self''。


欢迎各种帮助。




你找到了合适的地方。欢迎!

-

pkm~ http:// paulmcnett.com


Tim正确地指出我错过了我的

问题中最重要的部分。

i应该说__init __()不是自动调用继承层次结构的

。我们必须明确地明确调用所有基类

__init __()函数。

i想要一个理由。

谢谢Tim。


hi,
i come from a c++ background. i ws happy to find myself on quite
familiar grounds with Python. But, what surprised me was the fact that
the __init__(), which is said to be the equivlent of the constructor in
c++, is not automatically called. I''m sure there must be ample reason
for this. I would like to know why this is so? This is my view is more
burden on the programmer.
Similarly, why do we have to explicitly use the ''self'' keyword
everytime?

Every kind of help would be welcome.

解决方案

On 25 May 2005 21:31:57 -0700, Sriek <sc*******@gmail.com> wrote:

hi,
i come from a c++ background. i ws happy to find myself on quite
familiar grounds with Python. But, what surprised me was the fact that
the __init__(), which is said to be the equivlent of the constructor in
c++, is not automatically called. I''m sure there must be ample reason
for this. I would like to know why this is so? This is my view is more
burden on the programmer.

class C: .... def __init__(self): print "Hello"
.... c = C()


Hello

This looks like __init__ being called automatically to me. Are you
doing something different?
Similarly, why do we have to explicitly use the ''self'' keyword
everytime?
http://www.python.org/doc/faq/genera...ions-and-calls

Every kind of help would be welcome.
No worries,

Tim

--
http://mail.python.org/mailman/listinfo/python-list



Sriek wrote:

hi,
i come from a c++ background. i ws happy to find myself on quite
familiar grounds with Python. But, what surprised me was the fact that
the __init__(), which is said to be the equivlent of the constructor in
c++, is not automatically called.
What do you mean by automatically? :

Python 2.4.1 (#2, May 5 2005, 09:45:41)
[GCC 4.0.0 20050413 (prerelease) (Debian 4.0-0pre11)] on linux2
Type "help", "copyright", "credits" or "license" for more information.

class A(object): .... def __init__(self):
.... print "in __init__"
.... a = A()


in __init__

So __init__ is definitely called upon instantiation. It is true that if
you derive from A and override __init__, A.__init__ won''t be called
unless done so explicitly like:

class B(A):
def __init__(self):
print "in B.__init__()"
super(B, self).__init__()
I''m sure there must be ample reason
for this. I would like to know why this is so? This is my view is more
burden on the programmer.
It isn''t that much practical burden, and IMO it makes perfect sense.
When you override a method of a class, you want to have to explicitly
call superclass code, not have it run automatically, else you lose
control of the flow.

Similarly, why do we have to explicitly use the ''self'' keyword
everytime?
This is closer to a wart, IMO, but once you''ve used Python for a while
you''ll come to understand why this is so. Basically, everything in
Python is either a namespace or a name in a namespace. In the case of
the self reference which Python sends as the first arg automatically,
the method needs to bind that to a local name which is, by convention
only, ''self''.

Every kind of help would be welcome.



You''ve found the right place to hang out. Welcome!
--
pkm ~ http://paulmcnett.com


Tim pointed out rightly that i missed out the most crucial part of my
question.
i should have said that __init__() is not called automatically only for
the inheritance hierarchy. we must explicitly call all the base class
__init__() fuctions explicitly.
i wanted a reason for that.
Thanks Tim.


这篇关于__init __()不会自动调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆