【问题标题】:python __init__ argument becomes a tuplepython __init__ 参数变成一个元组
【发布时间】:2012-08-19 15:12:32
【问题描述】:

代码:

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 变成元组而不是字符串?

【问题讨论】:

    标签: python init


    【解决方案1】:

    因为这里是逗号:

    self.aa = aa,
    

    这是syntax for a tuple containing one element

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-12-06
      • 2010-11-28
      • 2013-03-27
      • 2011-01-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多