51

I accidentally overwrote set by using it as a variable name in an interactive python session - is there any way that I can get access to the original set function without just restarting my session?

(I have so much stuff in that session that I'd rather not have to do that, although of course I can if necessary.)

weronika
  • 2,561
  • 2
  • 24
  • 30
  • Does this answer your question? [How to get back an overridden python built-in function?](https://stackoverflow.com/questions/20885760/how-to-get-back-an-overridden-python-built-in-function) – Ta946 Oct 09 '22 at 11:41
  • @Ta946 It does, but my question was asked before that one and already has an answer, so I'm not sure why that's relevant? – weronika Oct 09 '22 at 15:26
  • i flagged the questions as duplicates. i guess that auto-generated my comment? wasn't expecting any activity on a 9yo question – Ta946 Oct 10 '22 at 16:15
  • 1
    Ah, makes sense! I'm occasionally still around on various stackexchange sites so it gives me activity notifications, it's not like I was checking this specifically XD – weronika Oct 11 '22 at 15:47

3 Answers3

86

Just delete the name that is masking the builtin:

>>> set = 'oops'
>>> set
'oops'
>>> del set
>>> set
<type 'set'>

You can always still access the original built-in through the builtins module (__builtin__ on Python 2, with underscores and no s); use this if you want to override the built-in but want to defer to the original still from the override:

>>> import builtins
>>> builtins.set
<type 'set'>

If you have trouble locating where the masking name is defined, do check all namespaces from your current one up to the built-ins; see Short description of the scoping rules? for what scopes may apply to your current situation.

user2357112
  • 260,549
  • 28
  • 431
  • 505
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • 2
    Doesn't work for me in Python 3. I get error that `open` is not being defined when I delete it. – Tomáš Zato Dec 31 '16 at 15:20
  • 1
    @TomášZato: then you never created a name in the current namespace that masks it. Without an example, I can't tell you more, perhaps you want to create new question for that? And please don't assume you have the exact same situation, please do *test your assumptions* in a new session before downvoting. What I describe in my answer works *exactly as shown* in Python 3. – Martijn Pieters Dec 31 '16 at 15:21
  • I'm sorry, I indeed misunderstood the situation. In my case, the open is actually replaced through the builtins module, not just aliased by local variable. – Tomáš Zato Dec 31 '16 at 15:33
  • @TomášZato: right, I've updated the question to explicitly call out that you need to check all scopes, including the built-in scope. – Martijn Pieters Dec 31 '16 at 16:43
10

You can use __builtin__:

>>> import __builtin__
>>> __builtin__.set
<type 'set'>

or simply(no imports required):

>>> __builtins__.set
<type 'set'>

For Python 3:

>>> import builtins
>>> builtins.set
<class 'set'>

From docs:

CPython implementation detail: Users should not touch __builtins__; it is strictly an implementation detail. Users wanting to override values in the builtins namespace should import the __builtin__ (no ‘s’) module and modify its attributes appropriately.

Ashwini Chaudhary
  • 244,495
  • 58
  • 464
  • 504
0

To use builtin wrapper, first assign its original address in a variable like X

After your work is done then set it to None and set back the original address to builtin function.

Example

  1. X= __builtin__.isinstance
  2. __builtin__.isinstance = myininstance
  3. work is done

    __builtin__.isinstance=None 
    
    __builtin__.isinstance=X 
    
Daniel Puiu
  • 962
  • 6
  • 21
  • 29
Sagar
  • 171
  • 1
  • 6
  • From a suggested edit: "WARNING: it is previous work. Preserve before you lose the builtin in the text of the code!" – pppery Feb 12 '22 at 00:55