Python 为什么map(str,self.my_list)在my_list的元素上调用u repr_u而不是u str_u?

Python 为什么map(str,self.my_list)在my_list的元素上调用u repr_u而不是u str_u?,python,Python,我已经通读了答案,每个人似乎都同意将str映射到一个列表应该调用元素。我已经仔细阅读了代码,列表正在正确生成 我的收藏包装如下所示: class Wrapper: def __init__(self, *args): self.foo = list(args) def __str__(self): return ", ".join(map(str, self.foo)) class MyType: def __init__(self,

我已经通读了答案,每个人似乎都同意将str映射到一个列表应该调用元素。我已经仔细阅读了代码,列表正在正确生成

我的收藏包装如下所示:

class Wrapper:
    def __init__(self, *args):
        self.foo = list(args)

    def __str__(self):
        return ", ".join(map(str, self.foo))
class MyType:
    def __init__(self, arg1, arg2, arg3):
        self.arg1 = arg1
        # set the other values

    def __repr__(self):
        return "I am a repr"

    def __str__(self):
        return "I am a str"
class SubWrapper(Wrapper):
    def __init__(self, definite_arg, *args):
        super().__init__(args)
        self.definite_arg = definite_arg

    def __repr__(self):
        return "I am a subclass repr"
从Wrapper.\uuuu str\uuuuuuuuuuuuuuuuuuuuuuuu,它跳转到所包含类中的uuuuuu repr\uuuuuuuuuuu。该类如下所示:

class Wrapper:
    def __init__(self, *args):
        self.foo = list(args)

    def __str__(self):
        return ", ".join(map(str, self.foo))
class MyType:
    def __init__(self, arg1, arg2, arg3):
        self.arg1 = arg1
        # set the other values

    def __repr__(self):
        return "I am a repr"

    def __str__(self):
        return "I am a str"
class SubWrapper(Wrapper):
    def __init__(self, definite_arg, *args):
        super().__init__(args)
        self.definite_arg = definite_arg

    def __repr__(self):
        return "I am a subclass repr"
当我使用调试器单步执行代码时,执行会直接从包装器中的映射行跳到MyType中的_repr__。如果我调用strwraper_instance.foo[0],我将跳转到uu str_u。为什么会这样

EDIT:Wrapper是被调用的实际类型的超类,尽管子类没有实现一个_str__。子类定义如下所示:

class Wrapper:
    def __init__(self, *args):
        self.foo = list(args)

    def __str__(self):
        return ", ".join(map(str, self.foo))
class MyType:
    def __init__(self, arg1, arg2, arg3):
        self.arg1 = arg1
        # set the other values

    def __repr__(self):
        return "I am a repr"

    def __str__(self):
        return "I am a str"
class SubWrapper(Wrapper):
    def __init__(self, definite_arg, *args):
        super().__init__(args)
        self.definite_arg = definite_arg

    def __repr__(self):
        return "I am a subclass repr"
为了遵守离题规则并回答有关如何调用代码的一些问题,我正在进行的具体调用包括:

>>> w = Wrapper(MyType(1, 2, 3), MyType(4, 5, 6))
>>> s = SubWrapper(MyType(7, 8, 9), MyType(1, 2, 3), MyType(4, 5, 6))
>>> print(str(w))
I am a str, I am a str
>>> print(str(s))
(I am a repr, I am a repr)
>>> print(", ".join(map(str, w.foo)))
I am a str, I am a str
>>> print(", ".join(map(str, s.foo)))
(I am a repr, I am a repr)

编辑_2:结果发现问题出在类-子包装的构造中。uuu init_uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu

但是,如果将每个元素映射到字符串,则它显然将使用字符串表示:

>>> ','.join(map(str, [MyType(), MyType()]))
'I am a str,I am a str'

>>> print(Wrapper(MyType(), MyType()))
I am a str, I am a str
因此,无论您在容器上使用str还是repr,内置容器(如列表)中的项目都将显示为带有_urepr_uu。您需要显式地转换每个项目,以便使用其_ustr__;方法显示它


1我从您的MyType中删除了_uinit__;方法。这只会使代码片段更短,卷积更少。

printWrapperMyType1,2,3,MyType1,2,3的工作原理与预期完全相同。您是否改为使用printWrapper[MyType1,2,3,MyType1,2,3]无法复制-如果我修复MyTypeSelf而不是self中的错误。然后做printWrapperMyType1,2,3,MyType4,5,6我知道我是一个str,我是一个str。我有一个正在运行的示例和它的输出供我们比较。@tdelaney和@donkopotamus,我在问题和解决方案中添加了一些更多的信息-我从来没有取消引用过,我肯定有一个python术语用于此,但我不知道,调用构造函数时会出现args。谢谢你的帮助。哎哟!很高兴它成功了。我称之为拆包。。。但我也不确定这是否是官方的。