Demonstrates private variables and methods : private « Class « Python Tutorial






class MyClass(object):
    def __init__(self, name, mood):
        print "A new MyClass has been born!"
        self.name = name            # public attribute
        self.__mood = mood          # private attribute

    def talk(self):
        print "\nI'm", self.name
        print "Right now I feel", self.__mood, "\n"

    def __private_method(self):
        print "This is a private method."

    def public_method(self):
        print "This is a public method."
        self.__private_method()

crit = MyClass(name = "Jack", mood = "happy")
crit.talk()
crit.public_method()








11.24.private
11.24.1.Class with private data members.
11.24.2.Demonstrates private variables and methods
11.24.3.Private Variables
11.24.4.Emulating Privacy for Instance Attributes