Posts: 11
Threads: 3
Joined: Mar 2020
Mar-26-2020, 02:19 PM
(This post was last modified: Mar-26-2020, 02:19 PM by dcollett.)
Hi, everyone. I'm happy to have found this forum. I believe this is a basic question about __init__, but I haven't been able to find an answer online. I'll appreciate any help and insight you can offer.
Q: Online I have seen the def __init__(self) statement for a class written with parameters that are initialized in the same line, and others that don't include parameters and initialize them in the body of the block. For example, please compare these two short snippets of code:
class Robot():
def __init__(self):
self.x = 0
self.y = 0
def move_robot(self, x_increment=1, y_increment=1):
self.x += x_increment
self.y += y_increment
robot01 = Robot()
robot01.move_robot(3,5)
print('Bot x,y =', robot01.x, robot01.y) and this one:
class Robot():
def __init__(self, x=0, y=0):
self.x = x
self.y = y
def move_robot(self, x_increment=1, y_increment=1):
self.x += x_increment
self.y += y_increment
robot01 = Robot()
robot01.move_robot(3,5)
print('Bot x,y =', robot01.x, robot01.y) They both produce the same output.
What's the difference?
Why in the first version do we use self.x = 0, but in the second version we initialize x and y in the __init__ line and then we use self.x = x?
Is there a reason, convention, advantage to using one method over another?
And what does it actually mean to say, self.x = x?
Thanks very much for your help!
David
Posts: 8,158
Threads: 160
Joined: Sep 2016
Mar-26-2020, 02:30 PM
(This post was last modified: Mar-26-2020, 02:30 PM by buran.)
in the first snippet, the start position (i.e. the position at the time when you create the instance) is always (0, 0).
In the second one you can pass x and/or y for start position., e.g.
class Robot():
def __init__(self, x=0, y=0):
self.x = x
self.y = y
def move_robot(self, x_increment=1, y_increment=1):
self.x += x_increment
self.y += y_increment
robot = Robot(x=4, y=2)
print('Bot x,y =', robot.x, robot.y) Output: Bot x,y = 4 2
if you don't pass x or y or both, default value of 0 will be used. So, in the second snippet if you don't pass x and y values, it will yield same result as first one.
If you look at move_robot() method you can pass x and/or y increment, different from default 1
Posts: 11
Threads: 3
Joined: Mar 2020
Hi, buran. Thanks. Your explanation helps. It seems, then, that the 2nd method is better because you have the option to initialize the object to the default values set, or you can pass in your own initial values. Is this correct?
Would there be any cases where the first method would be better to use?
Thanks for helping.
David
Posts: 8,158
Threads: 160
Joined: Sep 2016
It is up to you as programmer to decide, which one better suits your needs/use case. Given that second one is more universal, probably most would go with it in most cases. However, if you (for whatever reason) want to make it impossible to start from different start position than 0,0 - first one is your choice.
Posts: 99
Threads: 1
Joined: Dec 2019
(Mar-26-2020, 04:11 PM)buran Wrote: It is up to you as programmer to decide, which one better suits your needs/use case. Given that second one is more universal, probably most would go with it in most cases. However, if you (for whatever reason) want to make it impossible to start from different start position than 0,0 - first one is your choice. As a for instance. What if you wanted to track the mileage of your robots? You would calculate the distance travelled in your move_robot() method and add it to its mileage. BUT, every time you instantiate a brand new robot you would always want the mileage to be zero.
"So, brave knights, if you do doubt your courage or your strength, come no further, for death awaits you all with nasty, big, pointy teeth!" - Tim the Enchanter
Posts: 11
Threads: 3
Joined: Mar 2020
Mar-26-2020, 06:19 PM
(This post was last modified: Mar-26-2020, 07:02 PM by dcollett.)
buran and Marbelous,
Thank you both for your help. It makes much more sense now.
David
Marbelos, I reread (and tried) both versions again, this time creating a new robot. The initial x,y position of the new bot was 0,0.
So, in that respect, what you said would apply to either version, correct?
Thanks for helping.
David
Posts: 11
Threads: 3
Joined: Mar 2020
I also have a followup question about the second method:
class Robot():
def __init__(self, x=0, y=0): When I create a new object, I know that I can type either of these:
robot01 = Robot()
robot02 = Robot(3, 5) Is there a way when creating objects or calling functions that have multiple parameters to pass only a single value? For example, if I want to set an initial y value for a new robot, but I want to leave the default value (0) for the x, is there a way to do this? Or do I always need to pass none or all values?
Thanks!
David
Posts: 8,158
Threads: 160
Joined: Sep 2016
Mar-26-2020, 07:59 PM
(This post was last modified: Mar-26-2020, 08:09 PM by buran.)
(Mar-26-2020, 07:45 PM)dcollett Wrote: Is there a way when creating objects or calling functions that have multiple parameters to pass only a single value? For example, if I want to set an initial y value for a new robot, but I want to leave the default value (0) for the x, is there a way to do this? Or do I always need to pass none or all values?
you don't have to pass all of them. That is why I always said and/or.
robot = Robot(y=2) will instantiate a robot with x=0 and y=2.
Now, it's easy to check such things - just try
check https://docs.python.org/3.8/glossary.htm...-parameter
and https://docs.python.org/3.8/glossary.html#term-argument
Note the difference between positional and keyword parameters/arguments
let's say you have a __init__ method like this
def __init__(self, name, x=0, y=0):
self.name = name
self.x = x
self.y = y it has 4 parameters - self, name, x and y.
self and name are positional. self is a special case - as we are working with class, the first argument that is passed to function is the instance itself.
x and y are keyword parameters
so, you must always pass arguments for the positional parameters.
you MAY pass arguments for the keyword parameters. You can pass them as positional or keyword arguments, however if you want to skip some of the keyword parameters, then the rest should be passed mandatory as keyword arguments
robot = Robot('Robot1') - will create robot with name Robot1 and x=0, y=0
robot = Robot('Robot2', 1, 2) - will create robot with name Robot2 and x=1, y=2
robot = Robot('Robot3', 3) - will create robot with name Robot3 and x=3, y=0
robot = Robot('Robot4', y=4) - will create robot with name Robot4 and x=0, y=4
robot = Robot(x=5, y=6) - will raise error because of missing positional argument name
Posts: 11
Threads: 3
Joined: Mar 2020
Buran, ah! Now I get it. Thanks again to you both for your fast replies and help.
I was sailing along fine learning python basics until this __init__ and self popped up. The more I read, the more confused I became.
At least now, I think I have a very basic understanding of it.
Thanks.
Posts: 8,158
Threads: 160
Joined: Sep 2016
check my answer, I expanded it a bit in the meantime
|