Why does these attributes (commented) does not included in the init ?
class RRT(object):
def __init__(self, start, stop, obstacle_list, rand_area):
self.start = Node(start[0], start[1])
self.stop = Node(stop[0], stop[1])
self.min_rand = rand_area[0]
self.max_rand = rand_area[1]
self.exapandDis = 1.0 # just like this one
self.goalSampleRate = 0.05 # just like this one
self.maxIter = 500 # just like this one
self.obstacle_list = obstacle_list
CodePudding user response:
Have you ever heard of constructor?
The __init__
is a constructor in Python
.
A constructor is used to initialized member variables of a class. It is not necessary for a constructor to initialize each and every variable.
Sometimes, we want a constructor to initialize variables, which aren't passed in as parameters, with some default values.
It is not necessary for a construtor to only intialize the variables which are passed to it!
Thus, your code will initialize: exapandDis, goalSampleRate, maxIter
with default values 1.0, 0.05, 500
CodePudding user response:
You can give parameters default values. These are called "keyword arguments".
class RRT(object):
def __init__(self, start, stop, obstacle_list, rand_area,
exapandDis=1.0, goalSampleRate=.05, maxIter=500):
self.start = Node(start[0], start[1])
self.stop = Node(stop[0], stop[1])
self.min_rand = rand_area[0]
self.max_rand = rand_area[1]
self.exapandDis = expandDis # just like this one
self.goalSampleRate = goalSampleRate # just like this one
self.maxIter = maxIter # just like this one
self.obstacle_list = obstacle_list
Now, when someone creates an instance of the class, they must supply the positional arguments start, stop, obstacle_list, rand_area
but the others are optional. For instance, if you want to keep defaults expandDis and goalSapleRate but want a different maxIter, you would do
RRT(1, 100, [], 66, maxIter=1000)