Shilpa
Shilpa

Reputation: 1925

need help in understanding the code

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?

Upvotes: 0

Views: 187

Answers (3)

Tendayi Mawushe
Tendayi Mawushe

Reputation: 26118

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.

Upvotes: 2

Metalshark
Metalshark

Reputation: 8482

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.

Upvotes: 0

polygenelubricants
polygenelubricants

Reputation: 383726

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

Upvotes: 1

Related Questions