Search code examples
pythonhidemagic-methodsfacade

How to hide __methods__ in python?


I just wondered, how to hide special

__.*__ 

methods in python*? Especially I am using an interactive python interpreter with tab-completion, and I would like to display only the methods my modules expose ...

thanks,

/ myyn /

*(at least from the user, who uses a python shell)


it looks like this now:

h[2] >>> Q.
Q.ALL(                       Q.__delattr__(               Q.__getattribute__(                
Q.__package__                Q.__sizeof__(                Q.find_values(                         
Q.json
Q.DEFAULT_CONDITION(         Q.__dict__                   Q.__hash__(                  
Q.__reduce__(                Q.__str__(                   Q.get_loops_total_platform(  
Q.jsonlib
Q.SUCCESSFUL(                Q.__doc__                    Q.__init__(                  
Q.__reduce_ex__(             Q.__subclasshook__(          Q.get_platforms(             
Q.memoize(
Q.__all__                    Q.__file__                   Q.__name__                     
Q.__repr__(                  Q.cached_open(               Q.get_snippets(              
Q.__class__(                 Q.__format__(                Q.__new__(                      
Q.__setattr__(               Q.find_results(              Q.get_subjects(              
h[2] >>> Q.

and I wish it looked like:

h[2] >>> Q.
Q.ALL(                       Q.find_values(               Q.json
Q.DEFAULT_CONDITION(         Q.get_loops_total_platform(  
Q.jsonlib                    Q.SUCCESSFUL(                Q.get_platforms(             
Q.memoize(                   Q.cached_open(               Q.get_snippets(              
Q.find_results(              Q.get_subjects(              
h[2] >>> Q.

Solution

  • Well, you could create a subclass of rlcompleter.Completer, override the methods in question, and install that into readline.

    import rlcompleter
    import readline
    class MyCompleter(rlcompleter.Completer):
        def global_matches(self, text):
            ....
        def attr_matches(self, text):
            ....
    
    import readline
    readline.set_completer(MyCompleter().complete) 
    

    These code snippets allow case-insensitive tab completion:

    http://www.nabble.com/Re%3A-Tab-completion-question-p22905952.html