Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.
like image 381
miku Avatar asked Apr 23 '09 13:04

miku


People also ask

How do you hide a function in Python?

How do you hide a function in Python? Double underscore (__) can be used to hide. It can be added in front of the variable by doing this the function can be hidden while accessing.

What is __ method __ in Python?

__call__ in Python The __call__ method enables Python programmers to write classes where the instances behave like functions and can be called like a function. When the instance is called as a function; if this method is defined, x(arg1, arg2, ...) is a shorthand for x. __call__(arg1, arg2, ...) .

Are class methods hidden in Python?

In Python, there is no existence of Private methods that cannot be accessed except inside a class. However, to define a private method prefix the member name with the double underscore “__”.

How do you hide a variable in Python?

Object Oriented Programming in Python | Set 2 (Data Hiding and Object Printing) In Python, we use double underscore (Or __) before the attributes name and those attributes will not be directly visible outside.


1 Answers

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

like image 169
theller Avatar answered Sep 21 '22 06:09

theller