ruedi
ruedi

Reputation: 5545

Check the content of *args in a Class __init__

I want to test if in the *args parameter is a string ti. If so I want to print "ti". If it is a list where the first element has a length of 1 I want to print "it is a list with len 1 values". If both does not apply, I want to print "full list"

Here is my code it does not print anything and I do not know why. Can anyone help me here?

class MyClass:
    def __init__(self, *args):
        self.args = args

    def test_meth(self):

        if self.args == 'ti':
            print('ti')
        elif type(self.args) == list:
            if len(self.args[0]) == 1:
                print('it is a list with len 1 values')
            else:
                print('full list')

my_class = MyClass('ti')
my_class.test_meth()

Upvotes: 0

Views: 592

Answers (4)

N Chauhan
N Chauhan

Reputation: 3515

The args with always be a tuple, not a list, even if there’s only 1 element:

You don’t need to check whether the self.args is a tuple/list:

def test_meth(self):
    if len(self.args) == 1 and 'ti' in self.args:
        print('ti')
    if self.args and len(self.args[0]) == 1:
        print('list with length 1 len values')
    else:
        print('full list')

Upvotes: 3

vurmux
vurmux

Reputation: 10020

*args in def __init__(self, *args): will lead self.args to store a tuple so the first if-statement will always be False. If you want your code to work, you should rewrite it as:

class MyClass:
    def __init__(self, *args):
        self.args = args

    def test_meth(self):
        if self.args == ('ti',):
            print('ti')
        elif len(self.args) == 1 and len(self.args[0]) == 1:
            print('it is a list with len 1 values')
        else:
            print('full list')

my_class = MyClass('ti')
my_class.test_meth()

or with more generic (works with any iterable args):

class MyClass:
    def __init__(self, *args):
        self.args = args

    def test_meth(self):
        if len(self.args) == 1 and self.args[0] == 'ti':
            print('ti')
        elif len(self.args) == 1 and len(self.args[0]) == 1:
            print('it is a list with len 1 values')
        else:
            print('full list')

my_class = MyClass('ti')
my_class.test_meth()

Upvotes: 1

Bart Van Loon
Bart Van Loon

Reputation: 1510

self.args is, like @quamrana mentions, always of type tuple, and therefore never equal to 'ti' or of type list. That is why your current code doesn't print anything.

I believe the following does what you describe:

class MyClass:                                                              
    def __init__(self, *args):                                              
        self.firstarg = args[0]                                             

    def test_meth(self):                                                    
        if self.firstarg == 'ti':                                           
            print('ti')                                                     
        elif type(self.firstarg) == list:                                   
            if len(self.firstarg) == 1:                                     
                print('it is a list with 1 value')                          
            else:                                                           
                print('full list')                                          

my_object = MyClass('ti')                                                   
my_object.test_meth()                                                       
my_object = MyClass(['ti'])                                                 
my_object.test_meth()                                                       
my_object = MyClass(['ti', 'ti'])                                           
my_object.test_meth()

Hope this helps!

Upvotes: 0

figlio perduto
figlio perduto

Reputation: 170

type(my_class.args) is tuple so you can not compare it to a single value, i.e.

if self.args == 'ti':

However, you can do

if "ti" in self.args and len(self.args) == 1:

to check that "ti" is the only input.

Upvotes: 1

Related Questions