Nazim Moises
Nazim Moises

Reputation: 47

How can we write this in a single line loop?

In Python, is there more synthetic way to write this?

for n in dir():
    if n[0]!='_': delattr(sys.modules[__name__], n)

Upvotes: 0

Views: 91

Answers (1)

Chameleon
Chameleon

Reputation: 10138

That is very readable syntax do not change it.

for n in dir():
  if n[0]!='_': 
    delattr(sys.modules[__name__], n)

You can complicate but it has not sense - code must be simple first than second short.

More readable and short code is the best - not short and not readable :)

I will write it like this - even more readable for human:

for name in dir():
  if not name.startswith('_'): 
    delattr(sys.modules[__name__], name)

Upvotes: 2

Related Questions