You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python中ActorConfigurationData类rolename参数类型异常问题排查

问题:ActorConfigurationData实例化后rolename变为0且类型异常

调用ActorConfigurationData实例化对象前,rolename参数为字符串'ego_vehicle',执行以下代码后:

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

rolename的值变为0,类型不符合预期;但其他Python脚本调用该类时,rolename能正常返回字符串类型,需解决此问题确保rolename始终为字符串类型。

相关类代码如下:

#!/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

问题原因

核心问题是参数传递顺序错误:
ActorConfigurationData的__init__方法参数顺序为:model, transform, name=None, rolename='other', speed=0,...
你调用时的第三个参数rolename实际被赋值给了name参数,第四个参数speed被赋值给了rolename参数。如果speed的取值是0,就会导致self.rolename被设置为数值0,而非预期的字符串。

其他脚本调用正常,是因为它们要么使用了正确的参数顺序,要么通过关键字参数明确指定了参数对应关系。

解决方案

1. 修正参数传递方式

最稳妥的方式是使用关键字参数传递,彻底避免顺序错误:

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

如果坚持使用位置参数,需严格匹配__init__的参数顺序:

# 注意第三个参数是name(这里传None),第四个才是rolename
new_actor = ActorConfigurationData(model, None, None, rolename, speed, color=color, category=category, args=args)

2. 可选:添加类型校验

为防止后续出现类似问题,可在__init__中添加rolename的类型校验,确保其始终为字符串:

def __init__(self, model, transform, name=None, rolename='other', speed=0, autopilot=False,
             random=False, color=None, category="car", args=None):
    self.model = model
    # 强制校验rolename类型
    if not isinstance(rolename, str):
        raise TypeError(f"rolename必须是字符串类型,当前类型为{type(rolename).__name__}")
        # 若想兼容非字符串输入,可改为转换:rolename = str(rolename)
    self.rolename = rolename
    # 其余属性赋值逻辑不变...

内容的提问来源于stack exchange,提问作者Rony Shaji

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.15 11:07:14