Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Empty a variable without destroying it

I have this piece of code:

a = "aa"
b = 1
c = { "b":2 }
d = [3,"c"]
e = (4,5)
letters = [a, b, c, d, e]

And I want to do something with it, which will empty them. Without losing their type.

Something like this:

>>EmptyVars(letters)
['',0,{},[],()]

Any hint?

like image 733
f.rodrigues Avatar asked Jan 05 '14 22:01

f.rodrigues


1 Answers

Do this:

def EmptyVar(lst):
    return [type(i)() for i in lst]

type() produces the type object for each value, which when called produces an 'empty' new value.

Demo:

>>> a = "aa"
>>> b = 1
>>> c = { "b":2 }
>>> d = [3,"c"]
>>> e = (4,5)
>>> letters = [a, b, c, d, e]
>>> def EmptyVar(lst):
...     return [type(i)() for i in lst]
... 
>>> EmptyVar(letters)
['', 0, {}, [], ()]
like image 188
Martijn Pieters Avatar answered Sep 30 '22 02:09

Martijn Pieters