0

Can any one explain me about data hiding clearly ?

i didn't understand data hiding also with simple example apart from this tutorial in this app

13th Mar 2017, 1:36 PM
mohamed ibramsha
mohamed ibramsha - avatar
1 Answer
+ 4
The Basic concept of data hiding is making a member of a object invisible so you can't modify it. Python doesn't implement this like other languages and you can still access the 'hidden' member if you really want to. In c++ for example. if you have a private(aka hidden for your example) variable an instance of that class can not access that variable. It will thow an error. class AB { public: int x; protected: int y; private: int z; }; AB foo; //create an instance of the AB Class foo.x = 1 //allowed, can access x foo.z = 1 //error!, can't access z as it's 'hidden' using the private declaration In python there is no private, protected options. Instead we use a single or double underscore. Many ide's for python may warn if you try to access them however you still can. a very simplified way to think in python is when you see a single underscore on a variable/method it means you shouldn't access it directly, if you see a double underscore you really shouldn't access it directly...however python assumes you know what your doing if you do want to access it :) because we're grown ups. same but in python: class AB(object): def __init__(self): self.x = None # public, anyone can access it self._y = None # you shouldn't touch it self.__z = None # you really shouldn't touch it }; foo = AB(); //create an instance of the AB Class foo.x = 1 //allowed, can access x foo._y = 2 # allowed foo.__z = 3 # allowed You will notice the init function of the class also uses double underscores, this is slightly different but the in the same vain you could try: foo.__init__() But really you should never have to do that directly, it is 'hidden' for a reason. hope that helps and doesn't make it all the more confusing for you!!! Good luck!
14th Mar 2017, 3:22 AM
Elric Hindy
Elric Hindy - avatar