Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python __init__ argument becomes a tuple

Tags:

python

init

Code:

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')

Output:

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

Why does self.aa become a tuple and not a string?

like image 920
Yariv Avatar asked Nov 30 '22 05:11

Yariv


1 Answers

Beause of the comma here:

self.aa = aa,

This is the syntax for a tuple containing one element .

like image 191
Michael Mauderer Avatar answered Dec 06 '22 15:12

Michael Mauderer