class Person(object):
def __priva(self):
print "I am Private"
def publ(self):
print " I am public"
def callpriva(self):
self.__priva()
class Person(object):
def __priva(self):
print "I am Private"
def publ(self):
print " I am public"
def callpriva(self):
self.__priva()
>>>p>>> p._Person__priva I am Private
#test.py
def hello():
print "hello"
def _hello():
print "Hello private"
#----------------------
#test.py
def hello():
print "hello"
def _hello():
print "Hello private"
#----------------------
#test2.py
from test import *
print hello()
print _hello()
#test2.py
from test import *
print hello()
print _hello()
#test2.py
from test import _hello , hello
print hello()
print _hello()
#test2.py
from test import _hello , hello
print hello()
print _hello()
class Person(object):
def __priva(self):
print "I am Private"
def publ(self):
print " I am public"
def callpriva(self):
self.__priva()
>>>p._Person__priva
I am Private
#test.py
def hello():
print "hello"
def _hello():
print "Hello private"
#----------------------
#test2.py
from test import *
print hello()
print _hello()
#test2.py
from test import _hello , hello
print hello()
print _hello()
class Person(object):
def __priva(self):
print "I am Private"
def publ(self):
print " I am public"
def callpriva(self):
self.__priva()
>>> p._Person__priva
I am Private
#test.py
def hello():
print "hello"
def _hello():
print "Hello private"
#----------------------
#test2.py
from test import *
print hello()
print _hello()
#test2.py
from test import _hello , hello
print hello()
print _hello()
Python does not support privacy directly . Programmer need to know when it is safe to modify attribute from outside but anyway with python you can achieve something like private with little tricks. Now let's see a person can put anything private to it or not.
class Person(object): def __priva(self): print "I am Private" def publ(self): print " I am public" def callpriva(self): self.__priva()
Now When we will execute :
>>> p = Person()>>> p.publ() I am public>>> p.__priva() Traceback (most recent call last): File "", line 1, in p.__priva() AttributeError: 'Person' object has no attribute '__priva' #Explanation : You can see here we are not able to fetch that private method directly.>>> p.callpriva() I am Private #Explanation : Here we can access private method inside class
Then how someone can access that variable ???
You can do like :
>>>p._Person__priva I am Private
wow , actually if python is getting any variable starting with double underscore are "translated" by adding a single underscore and the class name to the beginning:
Note : If you do not want this name changing but you still want to send a signal for other objects to stay away, you can use a single initial underscore names with an initial underscore aren’t imported with starred imports (from module import *)
Example :
#test.py def hello(): print "hello" def _hello(): print "Hello private" #----------------------
#test2.py from test import * print hello() print _hello()
output-->
hello Traceback (most recent call last): File "", line 1, in NameError: name '_hello' is not defined
Now if we will call _hello manually .
#test2.py from test import _hello , hello print hello() print _hello()
output-->
hello hello private
Finally : Python doesn’t really have an equivalent privacy support, although single and double initial underscores do to some extent give you two levels of privacy