3

I know you're "not supposed to" use builtin names as parameters for functions, but sometimes they make the most sense:

def foo(range=(4,5), type="round", len=2):

But if this has been done, and the range variable has been processed and is no longer needed, how do I get back to the builtin range and use it inside foo()?

del range doesn't restore the builtin:

UnboundLocalError: local variable 'range' referenced before assignment
Community
  • 1
  • 1
endolith
  • 23,532
  • 31
  • 125
  • 187

2 Answers2

4

For Python 2.x

import __builtin__
range = __builtin__.range

For Python 3.x

import builtins
range = builtins.range
Jens
  • 7,396
  • 7
  • 52
  • 71
Ethan Furman
  • 57,917
  • 18
  • 142
  • 218
3

Also for both python versions, you can use __builtins__ without importing anything.

Example -

>>> def foo(range=(4,5)):
...     print(range)
...     range = __builtins__.range
...     print(range)
...
>>> foo()
(4, 5)
<class 'range'>
Anand S Kumar
  • 82,977
  • 18
  • 174
  • 164