派生/基类名称冲突 [英] derived / base class name conflicts

查看:73
本文介绍了派生/基类名称冲突的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设您想要编写某个现有类的子类,那么您将从一个您没有写过的模块导入
,并且您不想要
研究内部的,你想在你的

构造函数中定义一个数据成员。


如下所示:


来自module1 import A


class B(A):

def __init __(self):

A .__ init __(self)

self.i = 0

b = B()


现在,''我''可能已经被A或者通过调用来定义

A .__ init __()所以如果你在不知道的情况下定义它,你可能会改变A的行为。这种方法的方法不明确,这显然是件坏事。


避免这种情况的一种方法是运行以下程序清除名称

''我'先:

来自module1导入的
A

a = A()

打印ai


如果您获得了AttributeE rror,你知道名字''我'可以安全使用。如果

您实际上从打印声明中获得某种报告,那么您

将知道''我'不安全使用。


这让我觉得这是一个相当奇怪的程序,但是我没有看到任何方式。


还有其他一些方法来处理这个问题吗?


我真的需要在安全之前研究/实现

Tkinter.Canvas的来源将它子类化(并将属性

分配给子类的实例)或清除属性名称,如上所述

以上?


克里斯马歇尔

Suppose you want to write a subclass of some existing class you are
importing from a module you didn''t write and that you don''t want to
study the internals of, and you want to define a data member i in your
constructor.

As in the following:

from module1 import A

class B(A):
def __init__(self):
A.__init__(self)
self.i= 0
b= B()

Now, ''i'' might have already been defined by A or by the call to
A.__init__() so if you define it without knowing that, you could be
changing the behavior of A''s methods in unknown ways, which is
obviously a bad thing.

One way to avoid this is to run the following program to clear the name
''i'' first:

from module1 import A
a= A()
print a.i

If you get an AttributeError, you know the name ''i'' is safe to use. If
you actually get some sort of report from the print statement, then you
will know that ''i'' is not safe to use.

This strikes me as a rather odd procedure to go through, but I don''t
see any way around it.

It there some other way to handle this issue?

Do I actually need to study the sources for / implementation of
Tkinter.Canvas before I can safely subclass it (and assign attributes
to an instance of the subclass) or clear attribute names as I outlined
above?

Chris Marshall

推荐答案

这应该证明是最具启发性的:


import Tkinter

dir(Tkinter.Canvas)


2005年11月10日14:53:04 -0800, ch ****************** @ yahoo.com

< ch ****************** @ yahoo.com>写道:
This should prove most enlightening:

import Tkinter
dir(Tkinter.Canvas)

On 10 Nov 2005 14:53:04 -0800, ch******************@yahoo.com
<ch******************@yahoo.com> wrote:
假设你想要编写一个现有类的子类,你是从一个你没写过的模块导入的,你不想要
研究内部的,你想在你的
构造函数中定义一个数据成员。

如下所示:

来自module1 import A B / B类(A):
def __init __(自我):
A .__ init __(self)
self.i = 0
b = B()

现在,''我'可能已经被A或者通过调用A .__ init __()来定义,所以如果你在不知道的情况下定义它,你可能是
以不明的方式改变A'方法的行为,这显然是一件坏事。

避免这种情况的一种方法是运行以下程序来清除名称
''我'第一个:

来自module1导入A
a = A()
打印ai

如果你得到一个AttributeError,你知道名字''我'可以安全使用。如果你真的从打印声明中得到某种报告,那么你就会知道我的使用是不安全的。

这让我感到震惊通过相当奇怪的程序,但我没有看到它周围的任何方式。

有其他方法来处理这个问题吗?

我实际上需要研究/实现
Tkinter.Canvas的源代码才能安全地将其子类化(并将属性分配给子类的实例)或清除属性名称,如我所概述的那样
以上?

克里斯马歇尔

-
http://mail.python.org/mailman/listinfo/python-list
Suppose you want to write a subclass of some existing class you are
importing from a module you didn''t write and that you don''t want to
study the internals of, and you want to define a data member i in your
constructor.

As in the following:

from module1 import A

class B(A):
def __init__(self):
A.__init__(self)
self.i= 0
b= B()

Now, ''i'' might have already been defined by A or by the call to
A.__init__() so if you define it without knowing that, you could be
changing the behavior of A''s methods in unknown ways, which is
obviously a bad thing.

One way to avoid this is to run the following program to clear the name
''i'' first:

from module1 import A
a= A()
print a.i

If you get an AttributeError, you know the name ''i'' is safe to use. If
you actually get some sort of report from the print statement, then you
will know that ''i'' is not safe to use.

This strikes me as a rather odd procedure to go through, but I don''t
see any way around it.

It there some other way to handle this issue?

Do I actually need to study the sources for / implementation of
Tkinter.Canvas before I can safely subclass it (and assign attributes
to an instance of the subclass) or clear attribute names as I outlined
above?

Chris Marshall

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



-

Steve Juranich

图森,亚利桑那州

美国


--
Steve Juranich
Tucson, AZ
USA


ch ****************** @ yahoo.com 写道:
ch******************@yahoo.com wrote:
现在,''我'可能已经由A或通过调用A .__ init __()来定义,所以如果你在不知道的情况下定义它,你可能会改变A的行为。这种方法的方式不明,这显然是一件坏事。
Now, ''i'' might have already been defined by A or by the call to
A.__init__() so if you define it without knowing that, you could be
changing the behavior of A''s methods in unknown ways, which is
obviously a bad thing.



http://docs.python.org/tut/node11.ht...00000000000000


< / F>



http://docs.python.org/tut/node11.ht...00000000000000

</F>


Steve Juranich写道:
Steve Juranich wrote:
这应该证明最具启发性:

导入Tkinter
目录(Tkinter.Canvas)
This should prove most enlightening:

import Tkinter
dir(Tkinter.Canvas)




嗯?


Chris Marshall



Huh?

Chris Marshall


这篇关于派生/基类名称冲突的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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