Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I create an empty variable based on another data type in Python?

Tags:

python

How can I create empty variable that has the same data type of another variable?

   data = get_data()
   new_variable = data.data_type()  #just create empty variable

In the above case, if data is a list then new_variable will be list()

like image 503
Cory Avatar asked Sep 19 '25 14:09

Cory


1 Answers

Get the class of the value, invoke the constructor with no arguments, and hope that it doesn't blow up.

data.__class__()
like image 103
Ignacio Vazquez-Abrams Avatar answered Sep 22 '25 07:09

Ignacio Vazquez-Abrams