Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why was reload removed from python builtins in the switch to python3?

I recently made the switch from python 2 to python 3. Python 3 documentation reads: "Removed reload(). Use imp.reload()" It doesn't really say why though.

This question describes how it's done now in python 3. Does anyone have any idea why it's been removed from the built-ins and now requires imp or importlib to reload? When testing a program as you build it using the interactive prompt, having reload right there by default is super convenient. Obviously I can get that back by doing something like

from imp import reload

It's just another line of code every time I open an interactive prompt to test code. What's the reasoning behind this change?

like image 455
horta Avatar asked Jul 05 '15 20:07

horta


People also ask

How do I reload in Python 3?

In Python 3, reload was moved from builtins to imp. So to use reload in Python 3, you'd have to write imp. reload(moduleName) and not just reload(moduleName).

What is the use reload () in Python?

The reload() is used to reload a previously imported module or loaded module. This comes handy in a situation where you repeatedly run a test script during an interactive session, it always uses the first version of the modules we are developing, even we have made changes to the code.

What changed in Python 3?

Python 3.0 uses the concepts of text and (binary) data instead of Unicode strings and 8-bit strings. All text is Unicode; however encoded Unicode is represented as binary data. The type used to hold text is str , the type used to hold data is bytes .

How do I run Python 2 code in Python 3?

We can convert Python2 scripts to Python3 scripts by using 2to3 module. It changes Python2 syntax to Python3 syntax. We can change all the files in a particular folder from python2 to python3.


1 Answers

reload() was removed from the built-ins as one of the Python Regrets Guido expressed in a Keynote address at OSCON 2002 (slide 6). See PEP 3100 - Miscellaneous Python 3.0 Plans which names several changes that trace their origin to that talk.

From the slide I'd say he expected exec() to be used; that'd certainly cover the most common usecase of iteratively developing some code in an editor and re-testing it in an interactive interpreter.

However, when discussing PEP 3121 - Extension Module Initialization and Finalization Guido quickly found out that he missed the function:

Yes; I'm not certain whether module reloading continues to be supported in Py3k or not. If not, it should be removed from the PEP, if yes, it should be specified.

I'm already missing the reload() builtin, so I think it should be kept around in some form. I expect some form of reload functionality will remain available, perhaps somewhere in the imp module.

So, in short, reload() was removed first, then when it was missed, imp.reload() was added. It didn't really need to be a built-in anyway, I certainly use it only rarely.

In Python 3.4 the function was moved to importlib.reload().

like image 68
Martijn Pieters Avatar answered Oct 24 '22 06:10

Martijn Pieters