在这种情况下,抽象意味着什么?

发布于 2024-08-12 06:17:24 字数 650 浏览 4 评论 0原文

我需要一些帮助来理解 python 概念。

class TilePuzzleProblem(search.Problem):
""" This class is the class for the NxN - blanks tile puzzle problem """

    def __init__(self, N, blanks, initial, goal):
        """ Initialize """
        search.Problem.__init__(self, initial, goal)
        self.N = N
        self.blanks = blanks

    def successor(self, state):
        """ Generate the successors of the given state. Returns a list of (move, successor) pairs"""
        abstract

    def h(self, node):
        abstract

目前,代码挂在函数 h(...)abstract 部分,但我不知道 abstract 的含义,因此可以不明白问题是什么。

I need some help in understanding a python concept.

class TilePuzzleProblem(search.Problem):
""" This class is the class for the NxN - blanks tile puzzle problem """

    def __init__(self, N, blanks, initial, goal):
        """ Initialize """
        search.Problem.__init__(self, initial, goal)
        self.N = N
        self.blanks = blanks

    def successor(self, state):
        """ Generate the successors of the given state. Returns a list of (move, successor) pairs"""
        abstract

    def h(self, node):
        abstract

Currently the code hangs at the abstract part of the function h(...), but I have no idea what abstract means, hence can not understand what the problem is.

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

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

发布评论

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

评论(4

紅太極 2024-08-19 06:17:24

这是此处描述的技巧。 Python 中没有关键字 abstract,因此,如果您不在某个子类中重写此方法,则会导致 NotImplementedError

This is a trick described here. There's not keyword abstract in Python, so, if you won't override this method in some subclass, it'll cause NotImplementedError.

贪了杯 2024-08-19 06:17:24

抽象方法是类未实现的方法,使其成为抽象类;子类必须重写所有抽象方法(即提供具体实现)以成为具体类,即可以为其创建实例的类。 Python 中表达“此方法是抽象的”的正常方法是让方法的主体引发 NotImplementedError

有关一般概念的更多信息,除了 Python 中的具体细节之外,请参阅 wikipedia

因此,正式地,您需要对其进行子类化并实现标记为“抽象”的这两个方法。 (根据你的助教的提示,他或她实际上可能意味着你应该用代码的工作体替换“抽象”这个词,但这对于 OOP 中“抽象”的正常含义来说是相当大的延伸!-) 。

An abstract method is one which a class doesn't implement, making it an abstract class; subclasses must override all abstract methods (i.e., provide concrete implementations) to be concrete classes, i.e., ones for which you can make instances. The normal way in Python to express "this method is abstract" is to have the method's body be raise NotImplementedError.

For more about the general concept, apart from its specifics in Python, see wikipedia.

So, formally, you need to subclass this and implement those two methods marked as "abstract". (Depending on your TA's hints, he or she may actually mean that you should replace the word "abstract" with a working body of code, but that would be quite a stretch with respect the normal meaning of "abstract" in OOP!-).

我是有多爱你 2024-08-19 06:17:24

抽象函数是没有实现的函数。它是一个占位符,只是用来填写类契约,以便您知道子类应该提供哪些方法。您在这里需要做的是创建 TilePuzzleProblem 的后代并填写您自己的 h 实现。如果您想按原样运行它,请创建一个后代并使您的 h 不执行任何操作。

An abstract function is a function with no implementation. It's a placeholder, just there to fill out the class contract so that you know what methods subclass should provide. What you need to do here is create a descendant of TilePuzzleProblem and fill in your own implementation of h. If you want to run it as is, create a descendant and make your h do nothing.

执着的年纪 2024-08-19 06:17:24

抽象意味着该类必须被继承。抽象或“基”类提供了可以通过继承进行扩展的基类型。您不能实例化抽象类,只能实例化继承它的类。

请参阅这篇维基百科文章了解更多信息。

您想要使用抽象基类的原因之一是您想要对子类型进行分类或分组。例如,CarTruckPlane 都将从 Vehicle 抽象基类继承。你不能只实例化一个“车辆”,你必须实例化一辆汽车、卡车或飞机。抽象可以防止它被实例化。

Abstract means the class must be inherited. Abstract or "base" classes are there to provide a base type which you can extend through inheritance. You cannot instantiate an abstract class, only the classes that inherit it.

See this Wikipedia article for more information.

One reason you'd want to use an abstract base class is if you want to categorize or group your sub-types. For example, Car, Truck and Plane would all inherit from the Vehicle abstract base class. You can't just instantiate a "vehicle", you have to instantiate a car, truck or plane. Being abstract protects it from being instantiated.

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