pythonlistclasspycharmpositional-argument

Unable to use list for positional arguements


So I am working on a small project and I can't for the life of me figure our why this doesn't work....

I am using a list for positional arguments, yet it returns that parametres are missing, I know its probably something basic but I can't seem to figure it out..

If is just place the write out the list direction in function it works, but it doesn't seem to want to work with the contesetants list.

Hoping someone can help here!

class Tester():
    def __init__(self, first: int, second: int, third: int) -> None:
        self.first = first
        self.second = second
        self.third = third

contestants = [54, 56, 32]

print(Tester(contestants))

Solution

  • You need to unpack the list to pass the arguments:

    class Tester:
    
        def __init__(self, first: int, second: int, third: int) -> None:
            self.first = first
            self.second = second
            self.third = third
    
        def __str__(self) -> str:
            return f"Tester(first={self.first}, second={self.second}, third={self.third})"
    
    
    contestants = [54, 56, 32]
    print(Tester(*contestants))
    

    Output:

    Tester(first=54, second=56, third=32)