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

问题描述

限时删除!!



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

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

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



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

每次?


欢迎各种帮助。

解决方案



你好


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

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



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


不用担心,


Tim

-




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~



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.

解决方案


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




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



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

1403页,肝出来的..

09-06 19:55