Initialize variable depending on another variables type
<p>In Python 2.7 I want to intialize a variables type depending on another variable.</p>

<p>For example I want to do something like:</p>

<pre><code>var_1 = type(var_2)
</code></pre>

<p>Is there a simple/fast way to do that?</p>

<p>Just create another instance</p>

<pre><code>var_1 = type(var_2)()
</code></pre>

<p>Note that if you're not sure whether the object has a non-default constructor, you cannot rely on the above, but you can use <code>copy</code> or <code>deepcopy</code> (you get a "non-empty" object.</p>

<pre><code>import copy
var_1 = copy.copy(var_2)  # or copy.deepcopy
</code></pre>

<p>You could use both combined with the latter as a fallback mechanism</p>

<p>Note: <code>deepcopy</code> will ensure that your second object is completely independent from the first (If there are lists of lists, for instance)</p>

python python-2.7