Error: "self" is not defined error on VSCode on Ubuntu

Question:

I’m following a tutorial on understanding writing python publisher in ROS2. This is an example that I’m creating. The code does not seem to have any errors but in vscode, the self word is underlined in red and when I hover the mouse it shows that "self" is not defined. How can I resolve this issue in vscode?

I will add the code here

#!/usr/bin/env python3
import rclpy
from rclpy.node import Node
 
from example_interfaces.msg import String
 
class RobotNewsStationNode(Node):   #modify name
   def __init__(self):
       super().__init__("robot_news_station")  #modify name
 
       self.publisher_ = self.create_publisher(String, "robot_news", 10)
 
 
   def publish_news(self):
       msg = String()
       msg.data = "Hello"
       self.publisher_.publish(msg)
 
      
def main(args=None):
   rclpy.init(args=args)
   node = RobotNewsStationNode()   #modify name
   rclpy.spin(node)
   rclpy.shutdown()
  
if __name__ == "__main__":
   main()

This is the code error im having in vscode
enter image description here

Asked By: Dinoj

||

Answers:

As the error mentions, you are most likely mixing spaces/tabs.

Try delete all indentation untill that line, then use "tab" to indent your code, and be consistent about it i.e dont mix tabs and space.

Answered By: CutePoison