Open In App

Object Oriented Programming in Python | Set 2 (Data Hiding and Object Printing)

Last Updated : 07 Jun, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Prerequisite: Object-Oriented Programming in Python | Set 1 (Class, Object and Members)

 
Data hiding 

In Python, we use double underscore (Or __) before the attributes name and those attributes will not be directly visible outside. 

Python




class MyClass:
  
    # Hidden member of MyClass
    __hiddenVariable = 0
    
    # A member method that changes 
    # __hiddenVariable 
    def add(self, increment):
        self.__hiddenVariable += increment
        print (self.__hiddenVariable)
   
# Driver code
myObject = MyClass()     
myObject.add(2)
myObject.add(5)
  
# This line causes error
print (myObject.__hiddenVariable)


Output : 

2
7
Traceback (most recent call last):
  File "filename.py", line 13, in 
    print (myObject.__hiddenVariable)
AttributeError: MyClass instance has 
no attribute '__hiddenVariable' 

In the above program, we tried to access a hidden variable outside the class using an object and it threw an exception.
We can access the value of a hidden attribute by a tricky syntax: 

Python




# A Python program to demonstrate that hidden
# members can be accessed outside a class
class MyClass:
  
    # Hidden member of MyClass
    __hiddenVariable = 10
  
# Driver code
myObject = MyClass()     
print(myObject._MyClass__hiddenVariable)


Output : 

10

Private methods are accessible outside their class, just not easily accessible. Nothing in Python is truly private; internally, the names of private methods and attributes are mangled and unmangled on the fly to make them seem inaccessible by their given names [See this for source ]. 

 Printing Objects 

Printing objects give us information about objects we are working with. In C++, we can do this by adding a friend ostream& operator << (ostream&, const Foobar&) method for the class. In Java, we use toString() method.
In python, this can be achieved by using __repr__ or __str__ methods.

Python




class Test:
    def __init__(self, a, b):
        self.a = a
        self.b = b
  
    def __repr__(self):
        return "Test a:%s b:%s" % (self.a, self.b)
  
    def __str__(self):
        return "From str method of Test: a is %s," \
              "b is %s" % (self.a, self.b)
  
# Driver Code        
t = Test(1234, 5678)
print(t) # This calls __str__()
print([t]) # This calls __repr__()


Output : 

From str method of Test: a is 1234,b is 5678
[Test a:1234 b:5678]

Important Points about Printing: 

  • If no __str__ method is defined, print t (or print str(t)) uses __repr__. 

Python




class Test:
    def __init__(self, a, b):
        self.a = a
        self.b = b
  
    def __repr__(self):
        return "Test a:%s b:%s" % (self.a, self.b)
  
# Driver Code        
t = Test(1234, 5678)
print(t) 


Output :

Test a:1234 b:5678
  • If no __repr__ method is defined then the default is used. 

Python




class Test:
    def __init__(self, a, b):
        self.a = a
        self.b = b
  
# Driver Code        
t = Test(1234, 5678)
print(t) 


Output :

<__main__.Test instance at 0x7fa079da6710> 



Similar Reads

Data Hiding in Python
In this article, we will discuss data hiding in Python, starting from data hiding in general to data hiding in Python, along with the advantages and disadvantages of using data hiding in python. What is Data Hiding? Data hiding is a concept which underlines the hiding of data or information from the user. It is one of the key aspects of Object-Orie
3 min read
8 Tips For Object-Oriented Programming in Python
OOP or Object-Oriented Programming is a programming paradigm that organizes software design around data or objects and relies on the concept of classes and objects, rather than functions and logic. Object-oriented programming ensures code reusability and prevents Redundancy, and hence has become very popular even in the fields outside software engi
6 min read
Introduction of Object Oriented Programming
As the name suggests, Object-Oriented Programming or OOPs refers to languages that use objects in programming. Object-oriented programming aims to implement real-world entities like inheritance, hiding, polymorphism, etc in programming. The main aim of OOP is to bind together the data and the functions that operate on them so that no other part of
6 min read
Hiding and encrypting passwords in Python?
There are various Python modules that are used to hide the user’s inputted password, among them one is maskpass() module. In Python with the help of maskpass() module and base64() module we can hide the password of users with asterisk(*) during input time and then with the help of base64() module it can be encrypted. maskpass() maskpass() is a Pyth
6 min read
Python PyQt5 - Hiding the Progress Bar ?
In this article we will see how to hide the progress bar in PyQt5 application. There two ways in which we can hide the progress - Using hide method.Setting visibility status of progress bar to False. Method #1 : With the help of hide method, we can hide the progress bar. Below is the implementation. C/C++ Code # importing libraries from PyQt5.QtWid
2 min read
PyQt5 - Hiding the radio button
In this article we will see how we can hide the radio button, when we create a GUI(Graphical User Interface) we creates radio button but when their uses get complete there is a need to make these radio button hide so that user cant make them check or uncheck any more. In order to do to this we have to do the following: 1. Create radio button 2. Cre
2 min read
PyQt5 QSpinBox – Hiding the spin box
In this article we will see how we can hide the spin box, while making GUI(Graphical User Interface) when the use of widget get completed there is a need to remove the widget from the screen in order to do this we can hide the spin box. In order to do this we will use hide() method. Syntax : spin_box.hide() Argument : It takes no argument Return :
2 min read
PyQt5 QSpinBox – Hiding it using setHidden method
In this article we will see how we can make the spin box hidden with the help of sethidden method. We can hide the spin box using hide method as well but setHidden method takes bool as argument which determine whether it should hide the spin box or not when called. In order to do so we use setHidden method. Syntax : spin_box.setHidden(bool) Argumen
2 min read
PyQt5 QCalendarWidget - Hiding according to the user
In this article we will see how we can hide the QCalendarWidget according to the user, hiding is exactly opposite of showing the QCalendarWidget. Calendar widget is a big widget therefore there is need to hide it when user don't need it. In order to do this we will use hide method with the QCalendarWidget object.Syntax : calendar.hide()Argument : I
2 min read
PyQt5 QCalendarWidget - Hiding the Navigation Bar
In this article we will see how we can hide the navigation bar of the QCalendarWidget. Navigation bar is the top section of the calendar which is used to change the next month, previous month, month selection, year selection. By default navigation bar is visible although we can hide it any time, below is the representation of navigation bar of QCal
1 min read
Article Tags :
Practice Tags :