In Python, if I have an object, say x, then how do I create a new object y so that x and y are of the same class?
In Java, the code I want would look something like this:
Object y = x.getClass().newInstance();
You could probably do something like:
y = x.__class__()
or
y = type(x)() #New style classes only
And, if you're wondering how to make a new style class, all you need to do is inherit from object (or use python 3.x)
Pretty much the same as in Java:
y = x.__class__()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With