gpt4 book ai didi

python __init__ 参数变成一个元组

转载 作者:行者123 更新时间:2023-11-28 19:33:33 24 4
gpt4 key购买 nike

代码:

class MyClass:
def __init__(self, aa ):
print('aa='+str(aa)+' of type '+str(type(aa)))
self.aa = aa,
print('self.aa='+str(self.aa)+' of type '+str(type(self.aa)))

DEBUG = MyClass(aa = 'DEBUG')

输出:

aa=DEBUG of type <type 'str'>
self.aa=('DEBUG',) of type <type 'tuple'>

为什么self.aa变成元组而不是字符串?

最佳答案

因为这里的逗号:

self.aa = aa,

这是 syntax for a tuple containing one element .

关于python __init__ 参数变成一个元组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12027568/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com