I’m talking about import
at PyCon in April. In the talk, we’ll imagine that there is no import
and will reinvent it from scratch. I hope this will give everyone (including me!) a deeper understanding of the choices import
makes and the ways it could have been different. Ideally, the structure will be a couple of sections of the form “We could have made [decisions]. That would mean [effects]. Surprise – that’s how it works in [language]!”1
This is the first of (probably) several posts with notes of things I’m learning as I prepare my talk. Feedback is welcome.
Today I’m looking into Ruby’s require
and require_relative
2 to see if aspects of them would be interesting to Python programmers. So far, here’s what I think is most relevant:
Unlike Python,
require
won’t load all objects in the required file. There’s a concept of local versus global variables in the file scope that doesn’t exist in Python.Unlike Python, one file does not map to one module. Modules are created by using the keyword
module
.Unlike Python, namespace collisions are completely possible. Consider the following simple files:
1 2 3 4 5 |
|
1 2 3 4 5 |
|
1 2 3 4 |
|
And the output from running main.rb
:
1 2 3 |
|
- Like Python’s
import
,require
will only load a file once. This can interact interestingly with namespace collisions – to take a contrived example:
1 2 3 4 5 |
|
Because one.rb
isn’t reloaded, foo
is still 'world'
:
1 2 3 |
|
Questions for further investigation / thought
My talk should not convince people that Python is Right and other languages are Wrong. I’m trying to overcome my bias towards the system I’m most used to. (I think I’ve written roughly equal amounts of Python and Ruby, but the vast majority of the Ruby I’ve written is Rails, where all the require
ing and namespacing happens by magic.) Here are some questions I’d like to research more.
Python’s namespacing feels much better to me, although I’m sure that’s partly because I’m used to it. What’s the advantage to doing namespacing this way?
Why have both
require
andrequire_relative
? Why not haverequire
check the relative path as well before raising aLoadError
?What’s the advantage of uncoupling a module from a file?
-
I asked on twitter for suggestions of languages that make interesting decisions about
import
equivalents. So far the suggestions are R, Go, Rust, Ruby, JavaScript, and Clojure. If you have others, let me know.↩ -
As far as I can tell, the only difference between
require
andrequire_relative
is the load path searched.↩