0
$\begingroup$

I am a beginner, and I am following the ROS 2 tutorial. I have a couple of questions about this tutorial:

  • Q1: In line 1, we import rclpy, so why do we need to import Node separately in line 2?
  • Q2: In line 9, we write self as the argument of __init__ function. Can we write anything else instead of self? For example, can we define the function as: def __init__(us):?
  • Q3: In line 10, we give the name minimal_publisher. Can I write anything else instead of minimal_publisher in line 27? Can I write in line 27: test_minimal_publisher = MinimalPublisher()
  • Q4: What will be the name of our node? Is it MinimalPublisher or minimum_publisher?

Thank you for your time.

enter image description here

$\endgroup$

1 Answer 1

1
$\begingroup$

Q1: indeed, I don't think this is necessary. To be sure: try and remove it and see if it still works.

Q2: No, __init__(self) is the python equivalent of a constructor. It can have other arguments, but the first one is self. self is the equivalent of c++ this.

Q3: yes, it is just a variable name.

Q4: the node name is minimal_publisher, defined in line 10.

My suggestion for you is to read some tutorials on python first (just google, there are many), it'll make you advance much quicker.

Final remark: don't paste code as an image, paste the text in a code block (```), e.g.:

```python

[Code here]

```

which will look like this:


[Code here]

$\endgroup$
2
  • $\begingroup$ Thank you very much for all of your answers and suggestions. $\endgroup$
    – Msr09
    Commented Oct 19, 2023 at 18:51
  • 1
    $\begingroup$ I'd add on to the answer here. In Q1, you do not explicitly NEED to import rclpy.node, when you import rclpy, the Node class present in node.py should also be loaded. But, then in order to access the Node class, you'd have to do rclpy.node.Node every time. By importing the Node class separately from the rclpy.node module, you make it easier for you to access the Node class later on. It is also a good practice to import only what you need from any library, instead of doing something like from rclpy import *. $\endgroup$
    – ssarkar
    Commented Oct 20, 2023 at 9:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.