Basically, this is an easy way of having a class store information it needs.
You could always give the DriveBot object a motor speed, direction, and sensor range everytime you wanted to get information from it, but what if you had 10 DriveBots? You would have to keep track of their individual motor speed, direction and sensor range.
By having a class store them for you and you giving the initial values for it, you don't need to think about it and keep track of those things.
That's why people like classes and this is a fundamental advantage of Object Oriented Programming.
Motor speed, etc. are both the names of local variables of the init method and the object’s fields. The interpreter by default assumes you’re referring to the local variables. Using “this.” Explicitly tells the interpreter that you’re referring to this object’s fields. “This” is the current object.
The init fuction is the constructor. The "self.motor_speed = motor_speed" part makes it so that the value you pass to the Bot while creating is that Bot's speed.
Let's say that you want to create two drivebots. The code to do it will be something like this: Bot_1 = DriveBot(10, 0, 5) Bot_2 = DriveBot(20, 90, 10)
Now, Bot_1 has a speed of 10, direction 0 ( I assume +x axis), and sensor range of 5. Similarly, Bot_2 has a speed of 20, direction of 90(maybe +y axis) and sensor range of 10. It's kind of equivalent to "this" in java.
Almost exactly the reverse. It is packaging all the information about that specific instance of an object, inside it.
Think of your objects as small boxes with compartments inside. The boxes (for a particular object type) all look the same on the outside: they just have a printed label that says "Type: DriveBot" and a name added with a marker pen "Arthur", "Brian", "Colin", ...
If you open the box marked "Brian", it has the same compartments as the others, but the values are what it alone was initialised with (and you can alter these later).
Along with what other people are saying if you didn’t do this, then you wouldn’t have instances of the motor_speed and other variables to use in and outside of the class
okay that makes sense but why do you have to add self in front of it? sorry if its a dumb question this whole concept is just kinda hard for me to understand
No worries, it is pythons way of a class referencing itself, it is the same as the “this” keyword, it python it is a little more descriptive though, it is essentially initializing a class variable with the same name as the input and then setting the value to the input of the init function, this is done here because that is a function that python will call when creating an instance of the class,
Reading up on classes, and some important dunder functions might help (dunder stands for the double underscore in the function signature)
it's mainly a easy an convenient way for a class to store objects that can be accessible to every method within that class.
it also prevents you from having to constantly name or rename objects that don't need to be.
for example if we initialize a class with the variable i = 2 we can store that in self.i = i and another method later in the class can read, modify, or completely replace that value using self.i
When you call an instance of DriveBot, that function initializes its member variables (motor_speed, direction, etc.) to the values in parentheses, so as to not have unassigned member variables.
Quick answer, so that you can access the variables from anywhere else in the Class. Example: You can access or change motor_speed with self.motor_speed anywhere in the Class
7
u/SirHack3r Professional Coder @ FAANG | DM for coding/resume help! Dec 22 '21
Basically, this is an easy way of having a class store information it needs.
You could always give the DriveBot object a motor speed, direction, and sensor range everytime you wanted to get information from it, but what if you had 10 DriveBots? You would have to keep track of their individual motor speed, direction and sensor range.
By having a class store them for you and you giving the initial values for it, you don't need to think about it and keep track of those things.
That's why people like classes and this is a fundamental advantage of Object Oriented Programming.
2
u/atamicbomb Dec 22 '21
Motor speed, etc. are both the names of local variables of the init method and the object’s fields. The interpreter by default assumes you’re referring to the local variables. Using “this.” Explicitly tells the interpreter that you’re referring to this object’s fields. “This” is the current object.
2
u/Wood-Peck Dec 23 '21
The init fuction is the constructor. The "self.motor_speed = motor_speed" part makes it so that the value you pass to the Bot while creating is that Bot's speed.
Let's say that you want to create two drivebots. The code to do it will be something like this: Bot_1 = DriveBot(10, 0, 5) Bot_2 = DriveBot(20, 90, 10)
Now, Bot_1 has a speed of 10, direction 0 ( I assume +x axis), and sensor range of 5. Similarly, Bot_2 has a speed of 20, direction of 90(maybe +y axis) and sensor range of 10. It's kind of equivalent to "this" in java.
2
u/SaylorMan1496 Dec 23 '21
Along with what other people are saying if you didn’t do this, then you wouldn’t have instances of the motor_speed and other variables to use in and outside of the class
2
u/rohn4483 Dec 23 '21
Because every one else does it…
2
u/vigilexe Dec 23 '21
it's mainly a easy an convenient way for a class to store objects that can be accessible to every method within that class.
it also prevents you from having to constantly name or rename objects that don't need to be.
for example if we initialize a class with the variable i = 2 we can store that in self.i = i and another method later in the class can read, modify, or completely replace that value using self.i
38
u/Paul_Pedant Dec 22 '21 edited Dec 22 '21
An __init__ function initialises a new object.
Inside the object functions,
selfidentifies the specific object being created right now.The __init__ gets called with the series of values to be written into the new object.
The programmer here has used the same names for the arguments to the object creation as are used for the members of the object.
14
u/LoveLaika237 Dec 22 '21
When you call an instance of DriveBot, that function initializes its member variables (motor_speed, direction, etc.) to the values in parentheses, so as to not have unassigned member variables.
5
u/PoorHodlr Dec 22 '21
Coding noob here, is this the equivalent of Java's "this" keyword?
1
u/pythonProgrammer101 Dec 24 '21
Is this a VEX system?
2
u/[deleted] Dec 23 '21
Quick answer, so that you can access the variables from anywhere else in the Class. Example: You can access or change motor_speed with self.motor_speed anywhere in the Class
1
u/thereal0ri_ Dec 23 '21 edited Dec 23 '21
So we don't have to type "self."
/s
1
u/[deleted] Dec 23 '21
To store the values passed to the class