测试类中某个字段是否存在

发布于 2024-08-29 16:03:21 字数 272 浏览 4 评论 0原文

我有一个简单的问题。我有一个存储类实例的二维数组。根据程序前面读取的文本文件,为数组的元素分配一个特定的类。由于如果不查看文件,我就不知道特定元素中存储了什么类,所以我可以引用该索引中不存在的字段(指在该索引中存储 temp 实例时的外观)。我想出了一种测试方法,但它很冗长并且需要第二个矩阵。是否有一个函数可以测试类中某个字段是否存在?

class temp():
   name = "default"

class temp1():
   appearance = "@"

i have a quick question. I have a 2D array that stores an instance of a class. The elements of the array are assigned a particular class based on a text file that is read earlier in the program. Since i do not know without looking in the file what class is stored at a particular element i could refer to a field that doesn't exist at that index (referring to appearance when an instance of temp is stored in that index). i have come up with a method of testing this, but it is long winded and requires a second matrix. Is there a function to test for the existence of a field in a class?

class temp():
   name = "default"

class temp1():
   appearance = "@"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

凹づ凸ル 2024-09-05 16:03:21

hasattr(x, 'foo') 是一个内置的二进制函数,用于检查对象 x 是否具有属性 x.foo(是否从它的类中获取它与否),这似乎与您所要求的很接近。您所问的是否实际上是您应该问的则是另一个问题 - 正如@ Eli答案表明,你的设计看起来很奇怪。但是,这确实回答了您提出的问题。

hasattr(x, 'foo') is a built-in binary function that checks whether object x has an attribute x.foo (whether it gets it from its class or not), which seems close to what you're asking. Whether what you're asking is actually what you should be asking is a different issue -- as @Eli's answer suggests, your design seems strange. However, this does answer your question as asked.

够钟 2024-09-05 16:03:21

您是否在寻找:

isinstance(对象,类信息)

返回
true 如果对象参数是
classinfo 参数的实例,或者
(直接或间接)子类的
其中。如果 classinfo 也返回 true
是一个类型对象(新式类)并且
对象是该类型或的对象
(直接或间接)子类
其中。如果对象不是类
给定的实例或对象
类型,函数总是返回
错误的。如果 classinfo 既不是类
对象也不是类型对象,它可能是
类或类型对象的元组,或者可以
递归地包含其他这样的元组
(其他序列类型不
公认)。如果 classinfo 不是
类、类型或类的元组,
类型,以及这样的元组,一个 TypeError
引发异常。

无论你想做什么似乎都不是一个好主意。请更详细地描述您的原始需求,我们将帮助您提出更好的设计。

Are you looking for:

isinstance(object, classinfo)

Return
true if the object argument is an
instance of the classinfo argument, or
of a (direct or indirect) subclass
thereof. Also return true if classinfo
is a type object (new-style class) and
object is an object of that type or of
a (direct or indirect) subclass
thereof. If object is not a class
instance or an object of the given
type, the function always returns
false. If classinfo is neither a class
object nor a type object, it may be a
tuple of class or type objects, or may
recursively contain other such tuples
(other sequence types are not
accepted). If classinfo is not a
class, type, or tuple of classes,
types, and such tuples, a TypeError
exception is raised.

Whatever you're trying to do doesn't seem like a good idea. Please describe your original need in more detail, and we'll help you come up with a better design.

2024-09-05 16:03:21

您也可以使用异常处理来执行此操作。

try:
    val = x.name
except AttributeError:
    val = x.appearance

You could use exception handling to do this as well.

try:
    val = x.name
except AttributeError:
    val = x.appearance
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文