I was reading some code that looked basically like this:
class Foo(object):
class_name = __module__.replace('_', '-')
To me, that looked really weird (__module__
, what is that?) so I went and looked at the python data-model. A quick search shows that __module__
is a property of class objects and of function objects. However, there is no __module__
available in the global namespace (as can easily be verified by just trying to look at it and observing the NameError
that results ...).
I decided to chalk this up to implementation specific behavior, but as a last check, I decided to test with other implementations I have handy. It turns out that this code executes with1
- Cpython 2.7.6
- Cpython 3.4.0
- jython 2.5.3
- PyPy 2.2.1 (Python 2.7.3)
My question is whether this behavior is actually defined anywhere in the language reference. I'm not sure why I'd want to, but could I safely rely on __module__
being in the class creation namespace or did all the implementors just decide to do this the same way?
1All linux, but I doubt that matters ...