4

Experimenting with the Python interpreter, I unwittingly assigned a string to str as follows:

str = 'whatever'

Later in the same session I entered another statement with a call to str(), say...

double_whatever = str(2) + ' * whatever'

..., and got the error TypeError: 'str' object is not callable (instead of the expected output '2 * whatever'). A related SO answer helped me to quickly see the mistake I made.

However, I am still unclear how to fix calls to str() in the affected session. Of course I could exit the Python interpreter and start another session, but I am curious how to avoid that.

So far I have confirmed that...

double_whatever = __builtins__.str(2) + ' * whatever'  # => '2 * whatever'

...still works like I want; but I am unclear how to get back to not needing the __builtins__. qualification.

How can I fix my unintentional redefinition of str so that my calls to str() in the Python-interpreter session work again?

Community
  • 1
  • 1
J0e3gan
  • 8,740
  • 10
  • 53
  • 80
  • 3
    This recent SO answer will help: http://stackoverflow.com/a/30563742/3923281 – Alex Riley Jun 02 '15 at 08:40
  • Thanks. That looks spot-on, `str` not being the only candidate for the mistake it seems. I was actually working through an ORA online course that had me assign to `str`...and must have assumed a new session for each lecture. :P – J0e3gan Jun 02 '15 at 08:50
  • 1
    not the best, but quite trivial: `str = __builtins__.str` – Karoly Horvath Jun 02 '15 at 08:50
  • @KarolyHorvath: Yes, indeed. I tinkered with something close...and now see why it failed. I tried `str = __builtins__.str()`, which of course is not the same; but your suggestion was my intent. Thanks! – J0e3gan Jun 02 '15 at 08:57

1 Answers1

9

Just delete your overriding str:

del str

Read http://www.diveintopython.net/html_processing/locals_and_globals.html for an explanation of how Python finds a variable for you.

The basic idea is that the namespace where you define variables is searched first, so if you define a variable with the name str, the built-in one is overridden.

satoru
  • 31,822
  • 31
  • 91
  • 141
  • 5
    you beat me to earning a few easy points. :) – Pynchia Jun 02 '15 at 08:41
  • 1
    @Pynchia Let me share one upvote with you ;) – satoru Jun 02 '15 at 08:47
  • I am glad that my relative inexperience with Python could make for such sport. :) I have never come across the `del` keyword, and [it is a little odd](http://stackoverflow.com/q/6146963/1810429) to me based on more experience with other languages (but I guess no more odd than the ability to redefine built-in functions in JS): this worked perfectly as I am sure you quite confidently expected. Thanks! – J0e3gan Jun 02 '15 at 09:04
  • @satoru: thank you, but it's fine. You deserve it. I am sure I am not the only one who was about to click and.. laughed. I love SO for these things. :) – Pynchia Jun 02 '15 at 09:09
  • maybe, given @J0e3gan comment, you could expand your answer and explain why that works (namespace, etc.) – Pynchia Jun 02 '15 at 09:10
  • @Pynchia OK, let me have a try. – satoru Jun 02 '15 at 09:16
  • 2
    @J0e3gan I've updated the answer and add a link to a related chapter from Dive into Python. – satoru Jun 02 '15 at 09:22
  • Interestingly, much like I had tried something similar to Karoly Hovath's suggestion but not quite right, I had also tried something conceptually similar to `del str` but wrong: `str = None`! I distinctly sensed it would not work...but was familiar with `None`, had a mentalese self-dialog like, "...just want to *delete* that (`str`) var", and was bothered enough to try (as if there was any risk). Now I see that [`= None` is apparently not an uncommon awkward temptation](http://stackoverflow.com/q/6146963/1810429) in this sort of situation. What a great learning experience. Thanks again. – J0e3gan Jun 02 '15 at 09:31