tags:

views:

114

answers:

3
class Problem:

    """
    This class outlines the structure of a search problem, but doesn't implement
    any of the methods (in object-oriented terminology: an abstract class).
    """

    def getStartState(self):

         """Returns the start state for the search problem"""

         sahan.raiseNotDefined()

Now I want to know the meaning of above code? can I use the functions defined in the class in some other class functions as it is?

What does the class doc string mean?

+1  A: 

def getStartState(self) is a stubbed method; it's fully declared, but doesn't really "do" anything functional at the moment.

When invoked, it will raise an exception. Subclasses need to implement this method with actual functional code in order to get things to work properly.

See also

polygenelubricants
can I use this function of class in some other function?
Shilpa
It's not "useful" at the moment, but you can "use" it as it is; it'll just raise an exception. To make it more "use"-ful, you need to override it in a subclass with actual functionality. I recommend reading about basic OOP concepts, `abstract` classes, `interface`, etc.
polygenelubricants
can I use this function of class in some other function outside the class?
Shilpa
@Shilpa: read the freaking answer! **Subclasses need to implement this method with actual functional code in order to get things to work properly.**
SilentGhost
@Shilpa: you don't need this particular implementation of `getStartState`, but subclasses (which in all likelihood is something that YOU have to write yourself) will provide concrete implementations that actually do something useful. You don't need to change the code for `class Problem`; just extend and override `getStartState` in a subclass.
polygenelubricants
class foo(problem): def getStartState(self) return self.StartStateis it ok??
Shilpa
@Shilpa: You gotta do what you gotta do. I don't know _exactly_ what you need to do. It's best if you take a pause and read some textbooks/tutorials on OOP concepts.
polygenelubricants
ok...thanks alot
Shilpa
A: 
class Problem:

    """

    This class outlines the structure of a search problem, but doesn't implement
    any of the methods (in object-oriented terminology: an abstract class).

    """

    def getStartState(self):

        """

        Returns the start state for the search problem 

        """

        pass

Would allow you to use this class and denote that it is not yet defined.

By raising a notDefinedError you are explicitly stating that this code will fail when you try and use the class (instead of silently failing when you try and use its methods).

Python has a built-in exception for this called NotImplementedError.

class Problem:

    """

    This class outlines the structure of a search problem, but doesn't implement
    any of the methods (in object-oriented terminology: an abstract class).

    """

    def getStartState(self):

        """

        Returns the start state for the search problem 

        """

        raise NotImplementedError()

The class doc is basically stating that this is an interface to be followed, an abstract class, and that you are to either inherit class this or override the function there and then.

Metalshark
ButI am supposed to do no changes in this class? how can I use the functions of this class?
Shilpa
You are meant to either inherit this class from another class "class foo(Problem)" or add your own code to the getStartState method.
Metalshark
+2  A: 

This class is an attempt to define an abstract base class, this is what would be an Interface in Java or a class with only pure virtual methods in C++. Essentially it is defining the contract for a group of classes but not providing the implementation. The users of this class will implement the behaviour in subclasses. This class is an attempt to programmatically document an interface and make it clear that it cannot be used.

Creating interfaces that users will extend is good practice in general but it is commonly done when creating a framework. The core of the framework provides some useful common behaviour written to the interface, with users of the framework implementing the behaviour to achieve their specific goals.

Python being a dynamically typed language historically did not have direct support for abstract base class. However the need for them has always been tacitly acknowledged with some high profile frameworks providing their own support the concept. This idea has finally been formalised in the Abstract Base Classes (abc) standard library module.

Tendayi Mawushe
+1; very nice explanation!
polygenelubricants