Search code examples
pythonlistclassinstance-variables

Create an instance of a class passing values of list as arguments in Python


I'm currently try to pass the items of a list create an instance of a class.

My code looks like this

args = ["1", "John", "Doe"]

class Token:
    def __init__ = (self, id, name, last_name)
        self.id = id
        self.name = name
        self.last_name = last_name

instance1 = Token(args)

I get the error

TypeError: Token.__init__() missing 3 required positional arguments:

I tried changing the type, the data I received comes from a previous for loop that filters the data I need.

Expecting

To create an instance with the values of the list and pass them as args.

Note

The topple is currently in order


Solution

  • Use the iterable unpacking operator:

    Token(*args)