继承和内部/嵌套类 [英] Inheritance and Inner/Nested Classes

查看:66
本文介绍了继承和内部/嵌套类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望有人可以解释我为什么会遇到以下异常。

当我执行代码时...


### ###################################

class Parent(object):

类Foo(对象):

baz =''你好,来自Parent.Foo''


class Child(Parent):

#Foo.baz =''你好,来自Child.Foo''

通过


打印Child.Foo.baz

print dir(Child)

############################## ########


....它显示了我的期望...

来自Parent.Foo的
你好

[''Foo'',''__ class__'',''__ delattr__'',''_ _ _ _ _ _ _ _'''''''__ doc _''','/ b
''__ getattribute__' ',''__ hash__'',''__ init__'',''_ _ _ _ _ _ _''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''_'''' ',''__ setattr __'','_ _ _ _ _ _ _ _'','/ b $ b'' __weakref__'']


....但是当我取消注释Child类的第一行时,Python

抱怨Foo未定义...... <回溯(最近一次调用最后一次):

文件" test1.py",第5行,在?

class Child (家长):

文件test1.py,第6行,在孩子中

Foo.baz =''你好,来自Child.Foo''

NameError:名称''Foo''未定义


提前致谢。


Paul

I''m hoping that someone can explain why I get the following exception.
When I execute the code...

######################################
class Parent(object):
class Foo(object):
baz = ''hello from Parent.Foo''

class Child(Parent):
#Foo.baz = ''hello from Child.Foo''
pass

print Child.Foo.baz
print dir(Child)
######################################

....it displays what I expect...

hello from Parent.Foo
[''Foo'', ''__class__'', ''__delattr__'', ''__dict__'', ''__doc__'',
''__getattribute__'', ''__hash__'', ''__init__'', ''__module__'', ''__new__'',
''__reduce__'', ''__reduce_ex__'', ''__repr__'', ''__setattr__'', ''__str__'',
''__weakref__'']

....but when I uncomment the first line of the Child class, Python
complains that Foo is undefined...

Traceback (most recent call last):
File "test1.py", line 5, in ?
class Child(Parent):
File "test1.py", line 6, in Child
Foo.baz = ''hello from Child.Foo''
NameError: name ''Foo'' is not defined

Thanks in advance.

Paul

推荐答案

Paul Morrow写道:
Paul Morrow wrote:
############### #######################
class Parent(object):
class Foo(object):
baz =' '你好,来自Parent.Foo''

类Child(父母):
#Foo.baz =''你好,来自Child.Foo''
传递
######################################
class Parent(object):
class Foo(object):
baz = ''hello from Parent.Foo''

class Child(Parent):
#Foo.baz = ''hello from Child.Foo''
pass




在这里你想要Parent.Foo.bax = ...相反。


你究竟想做什么?这是不寻常的代码,

我认为...


-Peter



Here you want "Parent.Foo.bax = ..." instead.

What are you actually trying to do? This is unusual code,
I think...

-Peter


Peter Hansen写道:
Peter Hansen wrote:
Paul Morrow写道:
Paul Morrow wrote:
######################### #############
class Parent(object):
class Foo(object):
baz =''你好,来自Parent.Foo''

班级孩子(家长):
#Foo.baz =''你好,来自Child.Foo''
传递
######################################
class Parent(object):
class Foo(object):
baz = ''hello from Parent.Foo''

class Child(Parent):
#Foo.baz = ''hello from Child.Foo''
pass



在这里你想要Parent.Foo.bax = ...相反。

你究竟想做什么?这是不寻常的代码,我认为......

-Peter


Here you want "Parent.Foo.bax = ..." instead.

What are you actually trying to do? This is unusual code,
I think...

-Peter




我试图覆盖'' baz''在class.Foo

子类中的class属性,所以当我这样做时...


x1 = Parent.Foo()

x2 = Child.Foo()


.... x1和x2有不同的内部状态(

baz属性的值不同)。没有嵌套类,以下代码......


############################ #########

class父母(对象):

baz =''你好父母''


class Child(Parent):

baz =''你好孩子'


打印Parent.baz

打印Child.baz

#####################################


....产生我期望的......

来自父母的
你好
来自Child的br
你好/>

....但显然我的想法在嵌套类定义上是错误的。根据dir(Child)的说法,Foo是Child继承的,但是

我不能在Child的类定义中引用Foo 。即使我可以b / b
,它也不会是Child的父亲Foo的*副本*,而是

而不是父母的Foo本身---改变孩子的Foo.baz改变父母的Foo.baz

,这不是我想要的。


Thx。



I''m trying to override the ''baz'' class attribute in the Child.Foo
subclass, so that when I do...

x1 = Parent.Foo()
x2 = Child.Foo()

.... x1 and x2 have different internal states (different values for the
baz attribute). Without nested classes, the following code...

#####################################
class Parent(object):
baz = ''hello from Parent''

class Child(Parent):
baz = ''hello from Child''

print Parent.baz
print Child.baz
#####################################

....produces what I would expect...

hello from Parent
hello from Child

....but apparently my thinking is wrong on nested class definitions. It
seems odd that, according to dir(Child), Foo is inherited by Child, but
I can''t refer to Foo in Child''s class definition. And even if I
could, it wouldn''t be a *copy* of the Parent''s Foo that Child has, but
rather Parent''s Foo itself --- changing Foo.baz in Child changes Foo.baz
in Parent, which is not what I want.

Thx.


Paul Morrow写道:
Paul Morrow wrote:
Peter Hansen写道:
Peter Hansen wrote:
你究竟想做什么?这是不寻常的代码,我认为......
我正试图覆盖Child.Foo
子类中的''baz''类属性,所以当我这样做时。 ..

x1 = Parent.Foo()
x2 = Child.Foo()

... x1和x2有不同的内部状态(不同的值为
baz属性)。
What are you actually trying to do? This is unusual code,
I think...
I''m trying to override the ''baz'' class attribute in the Child.Foo
subclass, so that when I do...

x1 = Parent.Foo()
x2 = Child.Foo()

... x1 and x2 have different internal states (different values for the
baz attribute).




对不起,让我以不同的方式尝试这个问题。什么结束

目标,没有参考*你认为你想要实现它如何*
你想要实现
?你已经发布了很明显

人为的例子。我不能在这里看到这个目的...其他

比不同的内部状态,但如果那就是

你不要''需要有一个内部类来做它(因为你显示

你知道如下:)


没有嵌套类,下面的代码.. 。
#####################################
类Parent(对象) ):
baz =''你好父母''

上课孩子(家长):
baz =''你好,来自孩子'

打印Parent.baz
打印Child.baz
################################## ###

...产生我期望的......

亲们问好吧
亲爱的孩子

但显然我的想法在嵌套类定义上是错误的。根据dir(Child)的说法,Foo是Child继承的,但似乎很奇怪,但是我不能在Child的类定义中引用Foo。即使我可以,它也不会是孩子父母的Foo的副本*,而是父母的Foo本身---改变Foo.baz在Child中,改变了父母的Foo.baz,这不是我想要的。



Sorry, let me try the question in a different way. What end
goal, without reference to *how* you think you want to achieve it,
are you trying to achieve? You''ve posted what are obviously
contrived examples. I can''t see the purpose here... other
than "different internal states", but if that''s all it is
you don''t need to have an inner class to do it (as you show
you know by the following:)

Without nested classes, the following code...
#####################################
class Parent(object):
baz = ''hello from Parent''

class Child(Parent):
baz = ''hello from Child''

print Parent.baz
print Child.baz
#####################################

...produces what I would expect...

hello from Parent
hello from Child

...but apparently my thinking is wrong on nested class definitions. It
seems odd that, according to dir(Child), Foo is inherited by Child, but
I can''t refer to Foo in Child''s class definition. And even if I could,
it wouldn''t be a *copy* of the Parent''s Foo that Child has, but rather
Parent''s Foo itself --- changing Foo.baz in Child changes Foo.baz in
Parent, which is not what I want.




确实......那么*你想做什么?


顺便问一下,您是否考虑过直接在班级中使用*实例*而不是

?对于面向对象的编程来说,这是通常的

方式(可能也就是为什么它没有被称为面向类的编程的原因):-) :-) 。如果你这样做,你将使用初始化器__init __()通过在Parent'的内部创建一个Foo类的实例来设置不同的状态

br />
初始化程序,然后在调用父程序后,在

Child的初始化程序中进行适当的更改...


另一个问题是为什么要烦扰嵌套类?为什么

不仅仅是在两个班级之外坚持Foo? (这样做会因为
首先需要按照我在上一段中所建议的那样做,但是b $ b但是它可能会告诉你为什么你现在在做什么

有点奇怪,也许毫无意义。)


-Peter



Exactly... so what *do* you want?

By the way, have you considered just using *instances* instead
of mucking directly in the classes themselves? That''s the usual
way to go about object-oriented programming (and probably why it''s
not called "class-oriented programming" :-). If you did that, you
would use the initializer __init__() to set up the different state
by creating an instance of the Foo class inside the Parent''s
initializer, then making the appropriate changes inside the
Child''s initializer after calling the Parent''s...

Another question is why bother with the nested class? Why
not just stick Foo outside both classes? (Doing this would
first require doing what I suggested in the previous paragraph,
however...but it might show you why what you are doing now
is sort of weird and maybe pointless.)

-Peter


这篇关于继承和内部/嵌套类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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