In Python 2.7 I want to intialize a variables type depending on another variable.
For example I want to do something like:
var_1 = type(var_2)
Is there a simple/fast way to do that?
Just create another instance
var_1 = type(var_2)()
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 copy or deepcopy (you get a "non-empty" object.
import copy
var_1 = copy.copy(var_2) # or copy.deepcopy
You could use both combined with the latter as a fallback mechanism
Note: deepcopy will ensure that your second object is completely independent from the first (If there are lists of lists, for instance)
a = 1 # a is an int
a_type = type(a) # a_type now contains the int-type
b = '1' # '1' is a string
c = a_type(b) # c is now an int with the value 1
So you can get the type of a variable using type(). You can then store this type in a variable and you can then use that variable just like you would use int(b), str(b), float(b) etc.
isinstance()is usually a better solution thantype()(usually, not always).