如何检查类的继承? [英] How to examine the inheritance of a class?

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

问题描述

再次问好!


假设我有几个继承自基础

类的子类,因此:


class Foo(对象):


类Spam1(Foo):


类Spam2(Foo):


类Spam3(Foo):


等。子类列表未完全定义。它应该是用户可以扩展的。$ / b

许多方法在这些类之间会有所不同。但是,可以在两个Foo对象之间执行

操作,或者在$ F $ b之间执行任何Foo的衍生物。


有些实例我想在进行操作之前执行类型检查。

而不是必须枚举所有

Foo'的子类(无论如何都会破坏我扩展列表

子类的意图),我想看看是否一个来自Foo的课程是DERIVED

。这些信息存储在类定义中的哪个位置?


谢谢!

Hello again!

Suppose that I have several subclasses which inherit from a base
class, thus:

class Foo(object):

class Spam1(Foo):

class Spam2(Foo):

class Spam3(Foo):

etc. The list of subclasses is not fully defined. It is supposed to
be extensible by the user.

Many methods will differ between these classes. However, there are
operations which may be performed between two Foo objects, OR between
any of Foo''s derivatives.

There are instances where I would like to perform type checking before
carrying out the operation. Rather than having to enumerate all of
Foo''s subclasses (which would defeat my intent of extending the list
of subclasses anyway), I would like to see whether a class is DERIVED
from Foo. Where is this information stored in the class definition?

Thanks!

推荐答案

I忘了添加 - 虽然我怀疑它应该没关系 - 我在Ubuntu Linux 8.04上使用

Python 2.5.1。
I forgot to add -- though I suspect it should not matter -- I''m using
Python 2.5.1 on Ubuntu Linux 8.04.


2008年10月23日星期四下午6:36,John Ladasky< la ***** @ my-deja.comwrote:
On Thu, Oct 23, 2008 at 6:36 PM, John Ladasky <la*****@my-deja.comwrote:

再次问好!


假设我有几个继承自基础

类的子类,因此:


class Foo(对象):


类Spam1(Foo):


类Spam2(Foo):


class Spam3(Foo):


等。子类列表未完全定义。它应该是用户可以扩展的。$ / b

许多方法在这些类之间会有所不同。但是,可以在两个Foo对象之间执行

操作,或者在$ F $ b之间执行任何Foo的衍生物。


有些实例我想在进行操作之前执行类型检查。

而不是必须枚举所有

Foo'的子类(无论如何都会破坏我扩展列表

子类的意图),我想看看是否一个来自Foo的课程是DERIVED

。这些信息存储在类定义中的哪个位置?
Hello again!

Suppose that I have several subclasses which inherit from a base
class, thus:

class Foo(object):

class Spam1(Foo):

class Spam2(Foo):

class Spam3(Foo):

etc. The list of subclasses is not fully defined. It is supposed to
be extensible by the user.

Many methods will differ between these classes. However, there are
operations which may be performed between two Foo objects, OR between
any of Foo''s derivatives.

There are instances where I would like to perform type checking before
carrying out the operation. Rather than having to enumerate all of
Foo''s subclasses (which would defeat my intent of extending the list
of subclasses anyway), I would like to see whether a class is DERIVED
from Foo. Where is this information stored in the class definition?



在__bases__中,例如Spam1 .__ bases__,这将是(< class''_main __。Foo''>,)。

在实践中,你可能只想使用if isinstance(some_obj,

Foo):对于SpamN实例也是如此。


干杯,

克里斯

-

沿着鬣蜥的路径......
http://rebertia.com


>

谢谢!


-
< a rel =nofollowhref =http://mail.python.org/mailman/listinfo/python-listtarget =_ blank> http://mail.python.org/mailman/listinfo/python-list


2008年10月24日星期五上午11:36,John Ladasky< la ***** @ my-deja .comwrote:
On Fri, Oct 24, 2008 at 11:36 AM, John Ladasky <la*****@my-deja.comwrote:

等。子类列表未完全定义。应该可以由用户扩展

etc. The list of subclasses is not fully defined. It is supposed to
be extensible by the user.



开发者。不是用户。


考虑:

Developer. NOT User.

Consider:


这篇关于如何检查类的继承?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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