Can a python module have __repr__?

Can a python module have __repr__? The idea would be to do something like:

import mymodule print mymodule 

EDIT: accuracy: I mean custom .

+4
source share
5 answers

Short answer : basically the answer is no.

But can you not find the functionality you are looking for with docstrings?

  testmodule.py 
 """ my module test does x and y """ class myclass(object): ... 
  test.py 
 import testmodule print testmodule.__doc__ 

Long answer:

You can define your own __repr__ at the module level (just specify def __repr__(... ), but then you will need:

 import mymodule print mymodule.__repr__() 

to get the functionality you need.

Take a look at the following python shell session:

 >>> import sys # we import the module >>> sys.__repr__() # works as usual "<module 'sys' (built-in)>" >>> sys.__dict__['__repr__'] # but it not in the modules __dict__ ? Traceback (most recent call last): File "<stdin>", line 1, in <module> KeyError: '__repr__' >>> sys.__class__.__dict__['__repr__'] # __repr__ is provided on the module type as a slot wrapper <slot wrapper '__repr__' of 'module' objects> >>> sys.__class__.__dict__['__repr__'](sys) # which we should feed an instance of the module type "<module 'sys' (built-in)>" 

Therefore, I believe that the problem lies in these slot cover objects , which (from what can be read by reference) have the result of going around the usual "python" way of searching for item attributes.

For these class methods, CPython returns C pointers to the corresponding methods on these objects (which are then wrapped in slot cover objects that can be invoked by python).

+8
source

Modules may have a __repr__ function, but it is not called when a module view is received.

No, you cannot do what you want.

+4
source

You can achieve this effect - if you want to turn to the Dark side of the Force.

Add this to mymodule.py:

 import sys class MyReprModule(mymodule.__class__): def __init__(self, other): for attr in dir(other): setattr(self, attr, getattr(other, attr)) def __repr__(self): return 'ABCDEFGHIJKLMNOQ' # THIS LINE MUST BE THE LAST LINE IN YOUR MODULE sys.modules[__name__] = MyReprModule(sys.modules[__name__]) 

Lo and here:

 >>> import mymodule >>> print mymodule ABCDEFGHIJKLMNOQ 

I vaguely remember that in previous attempts to similar evil hacks there were problems setting special attributes such as __class__ . I did not have this problem when testing this. If you run into this problem, just catch the exception and skip this attribute.

+4
source

In fact, many modules have [ __repr__ ]!

 >>> import sys >>> print(sys) <module 'sys' (built-in)> #read edit, however, this info didn't come from __repr__ ! 

also try dir(sys) to see __repr__ there along with __name__ etc.

Edit :
__repr__ seems to be found in modules in Python 3.0 and above.
As Ned Batchelder pointed out, these methods are not used by Python when printing a module. (A quick experiment in which the repr property was reassigned showed that ...)

+2
source

No, because __repr__ is a special method (I call it ability), and it always looks at the class. Your module is just one more instance of a module type, so you would be able to determine __repr__ , it would not be called!

+1
source

All Articles