frazman
frazman

Reputation: 33213

constructor inheriting self python

I made a class which looks something like.

class NewInt:
  def __init__(self, x,y):
    self.holders = {"x":x,"y":y}

Now, I have a number say a dictionary. So everything is exactly same

except its not NewInt but a dict.Somethng like

 A  {"x": 3,"y":4}

So it is exactly of type NewInt except its a dictionary.

Is there a way i can do something like

this_int = NewInt(A)

I forgot but there was a special name to such constructor where assignment is done ?? Thanks

Upvotes: 1

Views: 117

Answers (1)

phihag
phihag

Reputation: 287755

You can use keyword arguments:

A = {"x": 3,"y":4}
this_int = NewInt(**A)

By the way, it's looking like you're reimplementing complex numbers. Those are already built in to Python, try complex(3,4) or 3+4j.

Upvotes: 4

Related Questions