make data/methids private

Discussion in 'Python' started by kangzhao, Nov 12, 2010.

  1. kangzhao

    kangzhao

    Joined:
    Nov 12, 2010
    Messages:
    1
    Location:
    Michigan
    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 kangzhao@yahoo.com.

    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
     
    kangzhao, Nov 12, 2010
    #1
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. qazmlp
    Replies:
    19
    Views:
    797
    Daniel T.
    Feb 4, 2004
  2. DaveLessnau
    Replies:
    3
    Views:
    428
    Howard
    May 16, 2005
  3. Jeremy Bowers
    Replies:
    3
    Views:
    516
    Steven Bethard
    Jan 24, 2005
  4. Daniel Finnie
    Replies:
    3
    Views:
    190
    Logan Capaldo
    Dec 16, 2006
  5. Frank Meyer

    Private methods not so private?

    Frank Meyer, Aug 1, 2007, in forum: Ruby
    Replies:
    14
    Views:
    259
    dblack@rubypal.com
    Aug 2, 2007
Loading...

Share This Page