基类是否可以知道方法是否已被覆盖? [英] Can a base class know if a method has been overridden?

查看:72
本文介绍了基类是否可以知道方法是否已被覆盖?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


我想知道这样的事情是否可行。一个基类

可以知道某个方法是否被子类覆盖了吗?

我很感激任何想法。

谢谢,


拉特科

Hi all,

I was wondering if something like this is possible. Can a base class
somehow know if a certain method has been overridden by the subclass?
I appreciate any ideas.
Thanks,

Ratko

推荐答案

Ratkoaécrit:
Ratko a écrit :

大家好,


我想知道这样的事情是否可行。一个基类

能否以某种方式知道某个方法是否已被子类覆盖?
Hi all,

I was wondering if something like this is possible. Can a base class
somehow know if a certain method has been overridden by the subclass?



如果你的用例是为了确保给定(''abstract'')方法已被覆盖,那么规范的解决方案是在

基类的实现中引发NotImplementedError,即:

class Parent(object):

def method(self):

引发NotImplementedError


类GoodGirl(父母):

def方法(个体经营):

打印我是个好姑娘


班BadBoy(家长):

通过

否则,这可能是可能使用自定义元类(或者可能只是

专门使用__new__方法),但可能有更好的解决方案

(取决于你真正想做的事情) ..


HTH

If your use case is to make sure a given (''abstract'') method has been
overriden, the canonical solution is to raise NotImplementedError in the
base class''s implementation, ie:
class Parent(object):
def method(self):
raise NotImplementedError

class GoodGirl(Parent):
def method(self):
print "I''m a good girl"

class BadBoy(Parent):
pass
Else, this may be possible using a custom metaclass (or possibly just
specializing the __new__ method), but there may be better solutions
(depending on what you''re really trying to do)..

HTH


9月24日下午5:23,Ratko< rjago ... @ gmail.comwrote :
On Sep 24, 5:23 pm, Ratko <rjago...@gmail.comwrote:

大家好,


我想知道这样的事情是否可行。一个基类

可以知道某个方法是否被子类覆盖了吗?

我很感激任何想法。

谢谢,


拉特科
Hi all,

I was wondering if something like this is possible. Can a base class
somehow know if a certain method has been overridden by the subclass?
I appreciate any ideas.
Thanks,

Ratko



我第一次使用Zope时,我立即遇到了覆盖一个问题的问题
预定的

方法不知道它(有400多种方法继承自

几十个基础

类)。所以我写了这个实用程序:

def check_if_I_am_overriding_names():

"""如果我们覆盖一个名字,则打印一条消息。对于

框架有用

初学者。 Zope中的示例:


The first time I used Zope, I immediately had an issue of overriding a
predefined
methods without knowing it (there were 400+ methods inherited from
dozens of base
classes). So I wrote this utility:
def check_if_I_am_overriding_names():
"""Prints a message if we are overriding a name. Useful for
framework
beginners. Example in Zope:


> from OFS.Folder import Folder
class MyFolder(OFS.Folder):
>from OFS.Folder import Folder
class MyFolder(OFS.Folder):



.. check_if_I_am_overriding_names()

.. id =''pippo''

..

AlreadyDefinedNameWarning:id

"""


def makecls(name,bases,dic):

代表nam,val代表dic.iteritems():

如果nam.endswith(" __")或nam ==" meta_types":

#忽略特殊名称的重新定义

#和meta_types的重新定义(对于Zope代码)

继续

any_base_has_name = [base in base in bases

如果hasattr(base,nam)]

if any_base_has_name:

print" AlreadyDefinedNameWarning:" + nam

返回类型(name,bases,dic)


f = sys._getframe(1)

f.f_locals [ " __ metaclass __"] = makecls


Michele Simionato

.. check_if_I_am_overriding_names()
.. id = ''pippo''
..
AlreadyDefinedNameWarning: id
"""

def makecls(name, bases, dic):
for nam, val in dic.iteritems():
if nam.endswith("__") or nam == "meta_types":
# ignore redefinitions of special names
# and redefinition of meta_types (for Zope code)
continue
any_base_has_name = [base for base in bases
if hasattr(base, nam)]
if any_base_has_name:
print "AlreadyDefinedNameWarning: " + nam
return type(name, bases, dic)

f = sys._getframe(1)
f.f_locals["__metaclass__"] = makecls

Michele Simionato


如果您的用例是确保给定的(''abstract'')方法一直是
If your use case is to make sure a given (''abstract'') method has been

overriden,规范解决方案是在

基类中引发NotImplementedError'实现
overriden, the canonical solution is to raise NotImplementedError in the
base class''s implementation



我真的不想强迫子类实现

方法。我有兴趣知道*是否*它确实实现了它或

没有。

I am not really interested in forcing the subclass to implement a
method. I am interested in knowing *whether* it did implement it or
not.


否则,这可能是可能的一个自定义元类(或者可能只是

专门用__new__方法),但可能有更好的解决方案

(取决于你真正想要做的事情)..
Else, this may be possible using a custom metaclass (or possibly just
specializing the __new__ method), but there may be better solutions
(depending on what you''re really trying to do)..



我有一个基类EvtHandler,它具有定义处理某些事件的方法

。然后,您从EvtHandler继承并覆盖要接收的事件的

方法。如果某个方法被覆盖了,那么基类将自动注册那些

事件,以确保它们甚至被传递给这个处理程序

(这就是为什么我需要知道一个方法是否已经被覆盖的b
)。当然,还有其他方法可以做到这一点

这需要从子类中做更多的工作...我只是

认为这将是一个整洁的自动 ;注册

活动的方式。


例如:


类EvtHandler:

def __init __(自我):

如果onKey被覆盖:

register_for_key_events()


def onKey(self):

通过

类MyHandler(EvtHandler):

def onKey(个体经营):

#在这做点什么。 ......

I have a base class EvtHandler that has methods defined to handle
certain events. You then subclass from EvtHandler and override the
methods for the events you want to receive. If a method has been
overridden, the base class will automatically register for those
events to make sure that they are even delivered to this handler
(which is why I would need to know whether a method has been
overridden or not). Of course, there are other ways of doing this
which would require a bit more work from the subclass... I just
thought this would be a neat "automatic" way of registering for
events.

For example:

class EvtHandler:
def __init__(self):
if onKey is overridden:
register_for_key_events()

def onKey(self):
pass
class MyHandler(EvtHandler):
def onKey(self):
# do something here....


这篇关于基类是否可以知道方法是否已被覆盖?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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