In an object oriented python program, you can restrict
access to methods and variables. This can prevent the data from being
modified by accident and is known as encapsulation. Let’s start with an
example.
The private attributes and methods are not really hidden, they’re renamed adding “_Car” in the beginning of their name.
Class with private variables. Variables can be private which can be useful on many occasions. A private variable can only be changed within a class method and not outside of the class.
class Car:
def __init__(self):
self.__updateSoftware()
def drive(self):
print 'driving'
def __updateSoftware(self):
print 'updating software'
redcar = Car()
redcar.drive()
#redcar.__updateSoftware() not accesible from object.
This program will output:
To learn Encapsulation in
Python at TCCI
.
Call us @ 98256 18292
For more information about programming Language , Computer course , Engineering course
Visit us @ http://tccicomputercoaching.com/course/
The private attributes and methods are not really hidden, they’re renamed adding “_Car” in the beginning of their name.
Class with private variables. Variables can be private which can be useful on many occasions. A private variable can only be changed within a class method and not outside of the class.
class Car:
def __init__(self):
self.__updateSoftware()
def drive(self):
print 'driving'
def __updateSoftware(self):
print 'updating software'
redcar = Car()
redcar.drive()
#redcar.__updateSoftware() not accesible from object.
This program will output:
updating software
driving |
Call us @ 98256 18292
For more information about programming Language , Computer course , Engineering course
Visit us @ http://tccicomputercoaching.com/course/
No comments:
Post a Comment