实例属性没有继承? [英] instance attributes not inherited?

查看:55
本文介绍了实例属性没有继承?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下简短程序失败:

----------------------- code --------- ---------------

#!/ usr / bin / python


class Parent(object):

def __init __(自我):

self.x = 9

print" Inside Parent .__ init __()"

class Child(Parent):

def __init __(self):

print" Inside Child .__ init __()"

p1 =父母()

p2 =父母()

c1 =孩子()

foo = [p1,p2,c1]

$ f $ b for f in foo:

print" x =" ;, ix

---------- ------- / code ----------------------


产生以下输出:


----------------输出------------------

Inside Parent .__ init __()

Inside Parent .__ init __()

Inside Child .__ init __()

x = 9
x = 9

x =

回溯(最近一次调用最后一次):

文件./ foo.py,第21行,在? />
print" x =",ix

AttributeError:''Child''对象没有属性''x''

--- ------------- /输出---------------------

为什么不是实例属性x是继承的?


我使用OOP的经验是使用C ++和(最近更多的是
)Java。如果我创建一个Child对象的实例,

我希望它*是*一个Parent对象(就像,如果我是子类

a Car class来创建一个VW类,我希望所有的大众汽车都能* *

汽车)。


也就是说,如果家长有什么可以做的话,不应该孩子能够做到这一点吗?考虑一个类似的程序:


------------------- code ------------- -----------

#!/ usr / bin / python

class父级(对象):

def __init __(self):

self.x = 9

print" Inside Parent .__ init __()"


def wash_dishes(个体经营):

print" Just wash",self.x," dishes。"

class Child(Parent):

def __init __(self):

print" Inside Child .__ init __()"

p1 = Parent()

p2 =家长()

c1 =孩子()

foo = [p1,p2,c1]

$ f $ b for f in foo:

i.wash_dishes()

------------------- / code -------- ---------------


但是失败了:


------ -------------输出----------------------

Inside Parent .__ init __()

Inside Parent .__ init __()

Inside Child .__ init __()

刚洗了9道菜。

Just洗了9道菜es。

刚刚洗过

Traceback(最近一次调用最后一次):

文件" ./ foo.py",第24行,在?

i.wash_dishes()

文件" ./ foo.py",第10行,在wash_dishes中

print" Just wash" ;,self.x,dish。

AttributeError:''Child''对象没有属性''x''

------ ------------- / output ---------------------


为什么不是这个继承的方法调用是否正常工作?

这是否是Python关于OO如何工作的概念的问题?


谢谢,
--- J


-

(如果发送电子邮件地址,请删除zeez)

解决方案

python'的oop继承没有什么问题,你只需要知道

,父类''__init__不会自动从

子类''__ init__。只需更改你的代码就可以完成这一步,你就可以了。

罚款:


class父级(对象):

def __init __(self):

self.x = 9

class Child(Parent):

def __init __(self):

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

print" Inside Child .__ init __()"


-David


John M. Gabriele写道:

以下短程序失败:

------- ----------------代码------------------------
#!/ usr / bin / python

类Parent(对象):
def __init __(self):
self.x = 9
print" Inside Parent .__ init __()" ;

class Child(Parent):
def __init __(self):
print" Inside Child .__ init __()"

p1 =家长()
p2 =家长()
c1 =孩子()
foo = [p1,p2,c1]

我在foo:
print" x =" ;, ix
----------------- / code ----------------------

产生以下输出:

----- -----------输出------------------
内部父.__ init __()
内部父.__ init __()
Inside Child .__ init __()
x = 9
x = 9
x =
Traceback(最近一次调用最后一次):
文件。 /foo.py" ;,第21行,在?
print" x =",ix
AttributeError:''Child''对象没有属性''x''
- -------------- / output ---------------------

为什么不是实例属性x是否继承?
我使用OOP的经验是使用C ++和(最近更多)Java。如果我创建一个Child对象的实例,
我希望它*是*一个Parent对象(就像我将一个Car类子类化为创建一个VW类,我希望所有大众'''' s to * be *
汽车)。

也就是说,如果有父母可以做的事情,不应该让孩子能够做到它也?考虑一个类似的程序:

------------------- code ----------------- -------
#!/ usr / bin / python

class父(对象):
def __init __(self):
self.x = 9
printInside Parent .__ init __()"

def wash_dishes(self):
print" Just wash,self.x,dishes。

class Child(Parent):
def __init __(self):
print" Inside Child .__ init __()"

p1 =父母()
p2 =父母()
c1 =孩子()
foo = [p1,p2,c1]

我在foo:
i.wash_dishes()
------------------- / code ------------------ -----

但失败了:

-------------------输出---- ------------------
Inside Parent .__ init __()
Inside Parent .__ init __()
Inside Child .__ init __()刚刚洗过9道菜。
刚刚洗过
Traceback(最近一次打电话):
文件。 /foo.py" ;,第24行,在?
i.wash_dishes()
文件" ./ foo.py",第10行,在wash_dishes中
打印刚刚洗过, self.x,dishes。
AttributeError:''Child''对象没有属性''x''
---------------- --- / output ---------------------

为什么这个继承的方法调用工作正常?
这是Python关于OO如何工作的概念的问题吗?

谢谢,
--- J




-

介绍:

平庸的星云。


David Hirschfield写道:< blockquote class =post_quotes> python'的oop继承没有什么问题,你只需要知道父类''__init__不是从
子类'__init__自动调用的。 。只需更改你的代码就可以完成这一步,你就会好的:

类父(对象):
def __init __(自我):
自我.x = 9

class Child(Parent):
def __init __(self):
super(Child,self).__ init __()
print"里面Child .__ init __()"

-David




如何帮助调用Parent .__ init__?那个电话只是简单的

会创建一个临时的Parent对象,对吗?我不知道它应该如何帮助(尽管*确实*确实有效)。


为什么我们需要自我传递调用super()?不应该
类名是否足以让super()找到合适的超类对象?


John M. Gabriele写道:< blockquote class =post_quotes> David Hirschfield写道:

python'的oop继承没有错,你只需要知道那个父类' '__init__不是从
子类''__ init__自动调用的。只需更改你的代码即可完成这一步,你就可以了。

类父(对象):
def __init __(自我):
自我.x = 9

class Child(Parent):
def __init __(self):
super(Child,self).__ init __()
print"里面Child .__ init __()"

-David



如何帮助调用Parent .__ init__?那个电话简单地会创建一个临时的Parent对象,对吗?我不知道它应该如何帮助(即使它确实*确实有效)。




对不起 - 我写的那个问题看起来像有点不完整:我的意思是

要问的是,它如何帮助这段代码工作:


---- code ----

#!/ usr / bin / python


class父(对象):

def __init __(self):

self.x = 9

print" Inside Parent .__ init __()"


def wash_dishes(self):

print" Inside Parent.wash_dishes(),washing",self.x," dishes。"

class Child(Parent):

def __init __(self):

super(儿童,自我).__ init __()

print" Inside Child .__ init __()"

c =孩子()

c.wash_dishes()

---- /代码----


自x在

super(Child,self).__ init __()调用期间创建的实例属性只是看起来的一部分

a tempora亲实例。



-

(如果发送电子邮件地址,请删除zeez)


The following short program fails:
----------------------- code ------------------------
#!/usr/bin/python

class Parent( object ):
def __init__( self ):
self.x = 9
print "Inside Parent.__init__()"
class Child( Parent ):
def __init__( self ):
print "Inside Child.__init__()"
p1 = Parent()
p2 = Parent()
c1 = Child()
foo = [p1,p2,c1]

for i in foo:
print "x =", i.x
----------------- /code ----------------------

yielding the following output:

---------------- output ------------------
Inside Parent.__init__()
Inside Parent.__init__()
Inside Child.__init__()
x = 9
x = 9
x =
Traceback (most recent call last):
File "./foo.py", line 21, in ?
print "x =", i.x
AttributeError: ''Child'' object has no attribute ''x''
---------------- /output ---------------------
Why isn''t the instance attribute x getting inherited?

My experience with OOP has been with C++ and (more
recently) Java. If I create an instance of a Child object,
I expect it to *be* a Parent object (just as, if I subclass
a Car class to create a VW class, I expect all VW''s to *be*
Cars).

That is to say, if there''s something a Parent can do, shouldn''t
the Child be able to do it too? Consider a similar program:

------------------- code ------------------------
#!/usr/bin/python
class Parent( object ):
def __init__( self ):
self.x = 9
print "Inside Parent.__init__()"

def wash_dishes( self ):
print "Just washed", self.x, "dishes."
class Child( Parent ):
def __init__( self ):
print "Inside Child.__init__()"
p1 = Parent()
p2 = Parent()
c1 = Child()
foo = [p1,p2,c1]

for i in foo:
i.wash_dishes()
------------------- /code -----------------------

But that fails with:

------------------- output ----------------------
Inside Parent.__init__()
Inside Parent.__init__()
Inside Child.__init__()
Just washed 9 dishes.
Just washed 9 dishes.
Just washed
Traceback (most recent call last):
File "./foo.py", line 24, in ?
i.wash_dishes()
File "./foo.py", line 10, in wash_dishes
print "Just washed", self.x, "dishes."
AttributeError: ''Child'' object has no attribute ''x''
------------------- /output ---------------------

Why isn''t this inherited method call working right?
Is this a problem with Python''s notion of how OO works?

Thanks,
---J

--
(remove zeez if demunging email address)

解决方案

Nothing''s wrong with python''s oop inheritance, you just need to know
that the parent class'' __init__ is not automatically called from a
subclass'' __init__. Just change your code to do that step, and you''ll be
fine:

class Parent( object ):
def __init__( self ):
self.x = 9
class Child( Parent ):
def __init__( self ):
super(Child,self).__init__()
print "Inside Child.__init__()"

-David

John M. Gabriele wrote:

The following short program fails:
----------------------- code ------------------------
#!/usr/bin/python

class Parent( object ):
def __init__( self ):
self.x = 9
print "Inside Parent.__init__()"
class Child( Parent ):
def __init__( self ):
print "Inside Child.__init__()"
p1 = Parent()
p2 = Parent()
c1 = Child()
foo = [p1,p2,c1]

for i in foo:
print "x =", i.x
----------------- /code ----------------------

yielding the following output:

---------------- output ------------------
Inside Parent.__init__()
Inside Parent.__init__()
Inside Child.__init__()
x = 9
x = 9
x =
Traceback (most recent call last):
File "./foo.py", line 21, in ?
print "x =", i.x
AttributeError: ''Child'' object has no attribute ''x''
---------------- /output ---------------------
Why isn''t the instance attribute x getting inherited?

My experience with OOP has been with C++ and (more
recently) Java. If I create an instance of a Child object,
I expect it to *be* a Parent object (just as, if I subclass
a Car class to create a VW class, I expect all VW''s to *be*
Cars).

That is to say, if there''s something a Parent can do, shouldn''t
the Child be able to do it too? Consider a similar program:

------------------- code ------------------------
#!/usr/bin/python
class Parent( object ):
def __init__( self ):
self.x = 9
print "Inside Parent.__init__()"

def wash_dishes( self ):
print "Just washed", self.x, "dishes."
class Child( Parent ):
def __init__( self ):
print "Inside Child.__init__()"
p1 = Parent()
p2 = Parent()
c1 = Child()
foo = [p1,p2,c1]

for i in foo:
i.wash_dishes()
------------------- /code -----------------------

But that fails with:

------------------- output ----------------------
Inside Parent.__init__()
Inside Parent.__init__()
Inside Child.__init__()
Just washed 9 dishes.
Just washed 9 dishes.
Just washed
Traceback (most recent call last):
File "./foo.py", line 24, in ?
i.wash_dishes()
File "./foo.py", line 10, in wash_dishes
print "Just washed", self.x, "dishes."
AttributeError: ''Child'' object has no attribute ''x''
------------------- /output ---------------------

Why isn''t this inherited method call working right?
Is this a problem with Python''s notion of how OO works?

Thanks,
---J



--
Presenting:
mediocre nebula.


David Hirschfield wrote:

Nothing''s wrong with python''s oop inheritance, you just need to know
that the parent class'' __init__ is not automatically called from a
subclass'' __init__. Just change your code to do that step, and you''ll be
fine:

class Parent( object ):
def __init__( self ):
self.x = 9
class Child( Parent ):
def __init__( self ):
super(Child,self).__init__()
print "Inside Child.__init__()"

-David



How does it help that Parent.__init__ gets called? That call simply
would create a temporary Parent object, right? I don''t see how it
should help (even though it *does* indeed work).

Why do we need to pass self along in that call to super()? Shouldn''t
the class name be enough for super() to find the right superclass object?


John M. Gabriele wrote:

David Hirschfield wrote:

Nothing''s wrong with python''s oop inheritance, you just need to know
that the parent class'' __init__ is not automatically called from a
subclass'' __init__. Just change your code to do that step, and you''ll
be fine:

class Parent( object ):
def __init__( self ):
self.x = 9
class Child( Parent ):
def __init__( self ):
super(Child,self).__init__()
print "Inside Child.__init__()"

-David



How does it help that Parent.__init__ gets called? That call simply
would create a temporary Parent object, right? I don''t see how it
should help (even though it *does* indeed work).



Sorry -- that question I wrote looks a little incomplete: what I meant
to ask was, how does it help this code to work:

---- code ----
#!/usr/bin/python

class Parent( object ):
def __init__( self ):
self.x = 9
print "Inside Parent.__init__()"

def wash_dishes( self ):
print "Inside Parent.wash_dishes(), washing", self.x, "dishes."
class Child( Parent ):
def __init__( self ):
super( Child, self ).__init__()
print "Inside Child.__init__()"
c = Child()
c.wash_dishes()
---- /code ----

since the x instance attribute created during the
super( Child, self ).__init__() call is just part of what looks to be
a temporary Parent instance.


--
(remove zeez if demunging email address)


这篇关于实例属性没有继承?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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