2

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

2 Answers 2

3

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)
1
  • 1
    Thanking you, will mark as correct answer once its allowed!
    – Brian
    Commented Oct 24 at 15:26
0

A list is a single value. You need to unpack the contents of the list, so that each element is passed as a separate argument:

print(Tester(*contestents))

This assumes that contestants has exactly 3 items. Too few or too many, and you'll get a TypeError.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.