why does the class returns different data types for the same program in Python?

46 Views Asked by At

I have a doubt with a Python class which is returning some arguments. In the following code, it is calling a python class ActorConfigurationData. The value of rolename before calling the class is 'ego_vehicle'.

new_actor = ActorConfigurationData(model, None, rolename, speed, color=color, category=category, args=args)

But after calling the class, the value becomes 0 and i have no idea how it become 0. Here is the class.

#!/usr/bin/env python


"""
This module provides the key configuration parameters for an XML-based scenario
"""

import carla


class ActorConfigurationData(object):

    """
    This is a configuration base class to hold model and transform attributes
    """

    def __init__(self, model, transform, name=None, rolename='other', speed=0, autopilot=False,
                 random=False, color=None, category="car", args=None):
        self.model = model
        self.rolename = rolename
        self.name = name
        self.transform = transform
        self.speed = speed
        self.autopilot = autopilot
        self.random_location = random
        self.color = color
        self.category = category
        self.args = args

        print('self.rolename')
        print(self.rolename)

    @staticmethod
    def parse_from_node(node, rolename):
        """
        static method to initialize an ActorConfigurationData from a given ET tree
        """

        model = node.attrib.get('model', 'vehicle.*')

        pos_x = float(node.attrib.get('x', 0))
        pos_y = float(node.attrib.get('y', 0))
        pos_z = float(node.attrib.get('z', 0))
        yaw = float(node.attrib.get('yaw', 0))

        transform = carla.Transform(carla.Location(x=pos_x, y=pos_y, z=pos_z), carla.Rotation(yaw=yaw))
        name = node.attrib.get('name', None)
        rolename = node.attrib.get('rolename', rolename)

        speed = node.attrib.get('speed', 0)

        autopilot = False
        if 'autopilot' in node.keys():
            autopilot = True

        random_location = False
        if 'random_location' in node.keys():
            random_location = True

        color = node.attrib.get('color', None)

        return ActorConfigurationData(model, transform, name, rolename, speed, autopilot, random_location, color)


class ScenarioConfiguration(object):

    """
    This class provides a basic scenario configuration incl.:
    - configurations for all actors
    - town, where the scenario should be executed
    - name of the scenario (e.g. ControlLoss_1)
    - type is the class of scenario (e.g. ControlLoss)
    """

    trigger_points = []
    ego_vehicles = []
    other_actors = []
    town = None
    name = None
    type = None
    route = None
    agent = None
    weather = carla.WeatherParameters()
    friction = None
    subtype = None
    route_var_name = None

Do you have any idea why it is happened? I tried with another python script and it is returning the type str which is expected.

To return a str datatype for the parameter 'rolename'

1

There are 1 best solutions below

0
h4z3 On

Let's look at the init and how you make the object:

new_actor = ActorConfigurationData(model, None, rolename, speed, color=color, category=category, args=args)
    def __init__(self, model, transform, name=None, rolename='other', speed=0, autopilot=False,
                 random=False, color=None, category="car", args=None):

Now let's take each argument in order.

Left is init order, right is creating the object:

  • self - automatically passed
  • model -> model
  • transform -> None
  • name -> rolename
  • rolename -> speed
  • and the rest is passed as keyword arguments, so they land where they should

You missed one argument when creating the object (name) and all the arguments passed as positional got shifted back.

Solution:

Option 1: Pass them as keyword arguments:

new_actor = ActorConfigurationData(model, None, rolename=rolename, speed=speed, color=color, category=category, args=args)

Bonus: you could set your function parameters to be keyword-only, so you can never mess those up again. But that could break usage in other parts of the code.

Option 2: Pass the missing name.