'Prevent Visual Studio Code or IDE From Revealing Python Class Private Methods
just want to ask a quick question;
Essentially, I want to know if it's possible to hide Python class private methods from the list of suggestions that Visual Studio Code or other IDEs provide.
For instance, suppose that we have a class "A":
# Creating a class
class A:
# Declaring public method
def fun(self):
print("Public method")
# Declaring private method
def __fun(self):
print("Private method")
# Calling private method via
# another method
def Help(self):
self.fun()
self.__fun()
Now, we don't want Visual Studio Code or other IDEs to show the method "__fun" in the list of suggestions provided by the IDE, how can we do that?
I can see that even though "__fun" has been declared as private method inside class "A", Visual Studio code will still suggest it in its snippet:
Would it be possible to get rid of this?
Solution 1:[1]
I may have found a partial solution to your question, which may help
There is a python language setting in vscode that, if enabled, reports an usage of 'protected' function (started with underscore) outside of the class.
Press CTRL+SHIFT+P, type "Configure Language Specific Settings" and select Python. In JSON object add the following property:
"python.analysis.diagnosticSeverityOverrides": {
"reportPrivateUsage": "error"
}
source: code.visualstudio.com/docs/python/settings-reference
Solution 2:[2]
You could simulate private methods using a method registration class but it will require tagging both the private methods and the public methods that are allowed to access them:
class PrivateMethods:
def __init__(self):
self.methods = dict()
def __call__(self,f):
name = inspect.getsource(f).split("def ",1)[-1]
name = name.split("(")[0].strip()
self.methods[name]=f # register private method
del(f) # and remove it from class
def access(self,f):
def withAccess(obj,*args,**kwargs):
obj.__inPrivate += 1 # internal depth counter
try: result = f(obj,*args,**kwargs) # around public calls
finally: obj.__inPrivate -= 1
return result
return withAccess
def method(self,instance,name):
if name == '_PrivateMethods__inPrivate': return 0
if name in self.methods and instance.__inPrivate:
f = self.methods[name]
return lambda *args,**kwargs:f(instance,*args,**kwargs)
else:
raise AttributeError(f'{type(instance)} has no {name} attribute')
You class will need to have an instance of PrivateMethods to hold the names and code of the private methods outside of the class itself. By defining the __getattr__ to access the private methods, you prevent any use of these methods outside of the class's code. The PrivateMethods object stores the name and code of private methods. It also removes them from the class (after being defined).
With this, you don't need to use the double underline naming convention to hide private methods (although it is still a good practice to do so)
Usage:
class myClass:
# boiler plate code to use in your class
private = PrivateMethods()
def __getattr__(self,name):
return myClass.private.method(self,name)
def privMethod(self,a):
print('in private',a,type(self))
private(privMethod) # register/remove private
@private.access # allow access to privates
def pubMethod(self):
self.privMethod("from pubMethod")
c = myClass()
c.pubMethod()
# in private from pubMethod <class '__main__.myClass'>
c.privMethod("from outside")
# AttributeError: <class '__main__.myClass'> has no privMethod attribute
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Dharman |
| Solution 2 |


