在这种情况下,抽象意味着什么?
我需要一些帮助来理解 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这是此处描述的技巧。 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 causeNotImplementedError
.抽象方法是类未实现的方法,使其成为抽象类;子类必须重写所有抽象方法(即提供具体实现)以成为具体类,即可以为其创建实例的类。 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!-).
抽象函数是没有实现的函数。它是一个占位符,只是用来填写类契约,以便您知道子类应该提供哪些方法。您在这里需要做的是创建
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 ofh
. If you want to run it as is, create a descendant and make yourh
do nothing.抽象意味着该类必须被继承。抽象或“基”类提供了可以通过继承进行扩展的基类型。您不能实例化抽象类,只能实例化继承它的类。
请参阅这篇维基百科文章了解更多信息。
您想要使用抽象基类的原因之一是您想要对子类型进行分类或分组。例如,
Car
、Truck
和Plane
都将从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
andPlane
would all inherit from theVehicle
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.