python - What' the meaning of the brackets in the class? -


in python, when read others' code, meet situation class defined , after there pair of brackets.

class astarfoodsearchagent(searchagent):      def __init__():         #.... 

i don't know meaning of '(searchagent)',because meet , use doesn't seem that.

it indicates astarfoodsearchagent subclass of searchagent. it's part of concept called inheritance.

what inheritance?

here's example. might have car class, , racecar class. when implementing racecar class, may find has lot of behavior similar, or same, car. in case, you'd make racecar subclass ofcar`.

class car(object):     #car subclass of python's base objeect. reasons this, , reasons why      #see classes without (object) or other class between brackets beyond scope      #of answer.      def get_number_of_wheels(self):         return 4      def get_engine(self):         return carengine(fuel=30)  class racecar(car): #racecar subclass of car     def get_engine(self):         return racecarengine(fuel=50)  my_car = car() #create new car instance desired_car = racecar() #create new racecar instance. my_car.get_engine() #returns carengine instance desired_car.get_engine() #returns racecarengine instance  my_car.get_number_of_wheels() #returns 4. desired_car.get_number_of_wheels() # returns 4! what?!?!?! 

we didn't define get_number_of_wheels on racecar, , still, exists, , returns 4 when called. that's because racecar has inherited get_number_of_wheels car. inheritance nice way reuse functionality other classes, , override or add functionality needs different.

your example

in example, astarfoodsearchagent subclass of searchagent. means inherits functionality searchagemt. instance, searchagent might implement method called get_neighbouring_locations(), returns locations reachable agent's current location. it's not necessary reimplement this, make a* agent.

what's nice this, can use when expect type of object, don't care implementation. instance, find_food function may expect searchagent object, wouldn't care how searches. might have astarfoodsearchagent , dijkstrafoodsearchagent. long both of them inherit searchagent, find_food can use ìsinstanceto check searcher expects behaves asearchagent. thefind_food`function might this:

def find_food(searcher):     if not isinstance(searcher, searchagent):         raise valueerror("searcher must 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 

old/classic style classes

upto python 2.1, old-style classes type existed. unless subclass of other class, wouldn't have parenthesis after class name.

class oldstylecar:     ... 

new style classes inherit something. if don't want inherit other class, inherit object.

class newstylecar(object):     ... 

new style classes unify python types , classes. instance, type of 1, can obtain calling type(1) int, type of oldstyleclass() instance, new style classes, type(newstylecar) car.


Comments

Popular posts from this blog

ruby on rails - RuntimeError: Circular dependency detected while autoloading constant - ActiveAdmin.register Role -

c++ - OpenMP unpredictable overhead -

javascript - Wordpress slider, not displayed 100% width -