views:

202

answers:

4

I'm completley new to python, so I'm having a hard time getting started with some code we got as a framework for a homework assignment.

I'll stress that I don't want help with doing the actual homework, only some help in understanding some python concepts.

here is the code snippet:

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 an abstract is so can not understand what the problem is.

could you help point me in the right direction here? (I'm not even sure what to google for.. "python abstract function"? "python def abstract"?)

Thanks!

p.s. It is quite likely (the TA hinted at this) that the abstract has no place here at all, but I'd like to know what I'm deleting before I delete it...

-Leav

A: 

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.

Soviut
thanks for the quick answer!why would someone use an abstract class? i'm trying to understand the reason for it's existance...
Leav
... and in the OP's example code, the `abstract` is simply a marker for people looking at it, that here is something missing (the real implementation).
Dirk
also, can there be abstract functions?
Leav
Why you'd want to use abstract classes: imagine a Shape class. Some possible methods would be GetArea, GetPerimeter, etc. However, unless you know what kind of shape you're dealing with, implementing these are impossible. Thus, they'd be abstract in the base Shape class, and be implemented in the subclasses (Rectangle, Triangle, etc). Look at the wikipedia entry on OOP.
Donnie
In Python2.6 there are many abstract base classes (ABCs) in module collections, for example `collections.MutableSet` -- the ABC defines many specific set operations, but you must subclass and implement the primitives for the set's internal storage yourself.
kaizer.se
abstract function does not really make sense, as they are not inherited and overridden, but you could interpret as static methods on a class? Or an abstract function in a module, where you have to "subclass" the module itself (by composition or somehow).
kaizer.se
+1  A: 

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.

Donnie
+5  A: 

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!-).

Alex Martelli
+6  A: 

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.

elder_george
`NameError`, actually. I think this trick may be the single worst idea I've seen all week.
Robert Rossney
Yes, terrible way of doing it. You should actually raise `NotImplementedError` yourself in the methods that need overriding.
Daniel Roseman
This makes me a sad (programming) panda.
Andrew Keeton
Also, don't be confused that Stack Overflow highlights `abstract` as if it were a keyword. The system that SO uses has to accommodate multiple programming languages, and in many `abstract` _is_ a keyword. But not in Python.
Andrew Keeton