D. Illouz
D. Illouz

Reputation: 1

Initializing a Class with varying arguments and varying parameters in Python

So I basically wanted to run a class but have the initialization vary depending on the amount of arguments I've provided.

Currently the class is run as such,

def __init__(self, *args): 
    if len(*args)==4:
        x=args[0]
        y=args[1]
        v=args[2]
        t=args[-1]
    if len(*args)==2:
        vx=args[0]
        vy=args[-1]
    if len(*args)==3:
        vx=args[0]
        vy=args[1]
        theta=args[-1]

    self.x=x
    self.y=y
    self.v=v
    self.t=t
    self.vx=vx
    self.vy=vy
    self.theta=theta

However, len(*args) only takes a single positional arguments and if I input Class(x,y,t,v) it won't run. I'd like to know the way I should start the initialization so I can have varying parameters.

I'm not supposed to split the class, according to the assignment given.

Thank you for any help.

Upvotes: 0

Views: 47

Answers (1)

Mitch
Mitch

Reputation: 3418

Replace *args with args in the if statements

if len(args)==3:

*args is a bit of a place holder, args is the actual tuple you care about

Upvotes: 1

Related Questions