make data/methids private

Joined
Nov 12, 2010
Messages
1
Reaction score
0
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top