I am trying to find a way to make some member data and member methods private in Python class. If someone finds the following code cannot do it, please make comments. Thanks. Comments are welcome to (e-mail address removed).
class A :
"""An example to hide the internal data and methods from user of a class."""
#user can see method work, it is empty
def work(self) : pass
#user can see the following constructor and member data x
def __init__(self, x) :
self.x = x
base = self.__class__
class B(base):
def __init__(self2, self) :
#y is a mamber data of B
self2.y = 1.0
#copy x in case user changes it
#self2.x = self.x
print "B.__init__"
def work(self2) :
print "B is working: x = " + str(self.x) + ", y = " + str(self2.y)
#create a local instance of B
b = B(self)
#b exists but user cannot see it through A. Therefore user cannot see member data y.
#A.work() is empty, we define it by B.work. Everything(non-built-in) in a class is a pointer.
self.work = b.work
class A :
"""An example to hide the internal data and methods from user of a class."""
#user can see method work, it is empty
def work(self) : pass
#user can see the following constructor and member data x
def __init__(self, x) :
self.x = x
base = self.__class__
class B(base):
def __init__(self2, self) :
#y is a mamber data of B
self2.y = 1.0
#copy x in case user changes it
#self2.x = self.x
print "B.__init__"
def work(self2) :
print "B is working: x = " + str(self.x) + ", y = " + str(self2.y)
#create a local instance of B
b = B(self)
#b exists but user cannot see it through A. Therefore user cannot see member data y.
#A.work() is empty, we define it by B.work. Everything(non-built-in) in a class is a pointer.
self.work = b.work