Reputation: 41
In python, when I read others' code, I meet this situation where a class is defined and after it there is a pair of brackets.
class AStarFoodSearchAgent(SearchAgent):
def __init__():
#....
I don't know what is the meaning of '(SearchAgent)',because what I usually meet and use doesn't seem that.
Upvotes: 0
Views: 2317
Reputation: 12867
It indicates that AStarFoodSearchAgent
is a subclass of SearchAgent
. It's part of a concept called inheritance.
Here's an example. You might have a Car
class, and a RaceCar
class. When implementing the RaceCar
class, you may find that it has a lot of behavior that is very similar, or exactly the same, as a Car
. In that case, you'd make RaceCar a subclass of
Car`.
class Car(object):
#Car is a subclass of Python's base objeect. The reasons for this, and the reasons why you
#see some classes without (object) or any other class between brackets is beyond the scope
#of this answer.
def get_number_of_wheels(self):
return 4
def get_engine(self):
return CarEngine(fuel=30)
class RaceCar(Car):
#Racecar is a subclass of Car
def get_engine(self):
return RaceCarEngine(fuel=50)
my_car = Car() #create a new Car instance
desired_car = RaceCar() #create a new RaceCar instance.
my_car.get_engine() #returns a CarEngine instance
desired_car.get_engine() #returns a RaceCarEngine instance
my_car.get_number_of_wheels() #returns 4.
desired_car.get_number_of_wheels() # also returns 4! WHAT?!?!?!
We didn't define get_number_of_wheels
on RaceCar
, and still, it exists, and returns 4 when called. That's because RaceCar
has inherited get_number_of_wheels
from Car
. Inheritance is a very nice way to reuse functionality from other classes, and override or add only the functionality that needs to be different.
In your example, AStarFoodSearchAgent
is a subclass of SearchAgent
. This means that it inherits some functionality from SearchAgemt
. For instance, SearchAgent
might implement a method called get_neighbouring_locations()
, that returns all the locations reachable from the agent's current location. It's not necessary to reimplement this, just to make an A* agent.
What's also nice about this, is that you can use this when you expect a certain type of object, but you don't care about the implementation. For instance, a find_food
function may expect a SearchAgent
object, but it wouldn't care about how it searches. You might have an AStarFoodSearchAgent
and a DijkstraFoodSearchAgent
. As long as both of them inherit from SearchAgent
, find_food
can use ìsinstanceto check that the searcher it expects behaves like a
SearchAgent. The
find_food`function might look like this:
def find_food(searcher):
if not isinstance(searcher, SearchAgent):
raise ValueError("searcher must be a SearchAgent instance.")
food = searcher.find_food()
if not food:
raise Exception("No, food. We'll starve!")
if food.type == "sprouts":
raise Exception("Sprouts, Yuk!)
return food
Upto Python 2.1, old-style classes were the only type that existed. Unless they were a subclass of some other class, they wouldn't have any parenthesis after the class name.
class OldStyleCar:
...
New style classes always inherit from something. If you don't want to inherit from any other class, you inherit from object
.
class NewStyleCar(object):
...
New style classes unify python types and classes. For instance, the type of 1
, which you can obtain by calling type(1)
is int
, but the type of OldStyleClass()
is instance
, with new style classes, type(NewStyleCar)
is Car
.
Upvotes: 3
Reputation: 368944
It means that SearchAgent
is a base class of AStarFoodSearchAgent
. In other word, AStarFoodSearchAgent
inherits from SearchAgent
class.
See Inheritance - Python tutorial.
Upvotes: 0
Reputation: 824
This is inheritance in python, just like in any other OO language
https://docs.python.org/2/tutorial/classes.html#inheritance
Upvotes: 0
Reputation: 7290
It means that class AStarFoodSearchAgent extends SearchAgent.
Check section 9.5 here
https://docs.python.org/2/tutorial/classes.html
Upvotes: 0
Reputation: 1048
SearchAgent
is the superclass of the class AStarFoodSearchAgent
. This basically means that an AStarFoodSearchAgent
is a special kind of SearchAgent
.
Upvotes: 0