Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it bad to name a Python module "global" or other keyword?

Tags:

python

django

I inherited some old Django code where one of the modules is named global (full name in INSTALLED_APPS being 'labweb.global'), which holds the models and views that drive the front page and some other scattered parts of the the site. However, global is a Python keyword, so this smells...but it works.

I'm ~99% sure that it's a bad idea to name modules after keywords, but I'm somewhat amazed that it works at all. How does Django not seem to care?

like image 258
Nick T Avatar asked Aug 31 '25 18:08

Nick T


1 Answers

Yes, of course it's a Bad Idea. Of course you can't import such a module directly:

import global # or any other keyword

just raises SyntaxError at compile-time. But perhaps that's the point? That is, perhaps the designer wanted to make sure the module couldn't be imported directly (but only via trickery). I don't know - I'm just trying to be generous there ;-)

Example

This is probably the easiest way to import such a module. Here's file global.py:

print "I'm global!"

And then:

>>> import importlib
>>> importlib.import_module("global")
I'm global!
<module 'global' from 'global.py'>

There are other ways to do it. There's simply no way to import directly, though.

like image 106
Tim Peters Avatar answered Sep 02 '25 08:09

Tim Peters