Object Oriented Programming in Python | Set 2 (hiding data and printing objects)

| |

Prerequisite:

class MyClass:

# Hidden member MyClass

__ hiddenVariable = 0

# 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 raises an 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 access a hidden variable outside the class using an object and it threw an exception.

We can access the value of a hidden attribute using a tricky syntax:

# Python program to demonstrate what’s hidden
# members can be accessed outside the class

class MyClass:

# Hidden member MyClass

__ hiddenVariable = 10


# Driver code

myObject = MyClass ()

print (myObject._MyClass__hiddenVariable)

Output:

 10 

Private methods are available outside of their class, but not as easily accessible. Nothing in Python is truly private; internally, the private method and attribute names are mapped and unmangled on the fly to make them appear inaccessible by their given names [see this for the source].

Printing objects

Printing objects gives us information about the objects with which we work. In C++, we can do this by adding a friend ostream & amp; operator

class Test:

def __ init __ ( self , a, b):

self . a = a

self . b = b

def __ repr __ ( self ):

retu rn "Test a:% sb:% 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 __ ()

Exit:

 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__.

    class Test:

    def __ init __ ( self , a, b):

    self . a = a

    self . b = b

    def __ repr __ ( self ):

    return " Test a:% sb:% 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, the default is used.

    class Test:

    def __ init __ ( self , a, b):

    self . a = a

    self . b = b


    Driver code

    t = Test ( 1234 , 5678 )

    print (t)

    Output:

This article courtesy of Shwetanshu Rohatgi . Please post comments if you find something wrong or if you would like to share more information on the topic discussed above.

हमें उम्मीद है कि इस लेख ने समस्या को हल करने में आपकी मदद की है। Object Oriented Programming in Python | Set 2 (hiding data and printing objects) के अलावा, अन्य File handling-संबंधित विषयों की जाँच करें।

पायथन में उत्कृष्टता प्राप्त करना चाहते हैं? सर्वश्रेष्ठ पायथन ऑनलाइन पाठ्यक्रम 2022 की हमारी समीक्षा देखें। यदि आप डेटा विज्ञान में रुचि रखते हैं, तो यह भी देखें कि R में प्रोग्रामिंग कैसे सीखें

वैसे, यह सामग्री अन्य भाषाओं में भी उपलब्ध है:

<उल>
  • Object Oriented Programming in Python | Set 2 (hiding data and printing objects)
  • Italiano Object Oriented Programming in Python | Set 2 (hiding data and printing objects)
  • Deutsch Object Oriented Programming in Python | Set 2 (hiding data and printing objects)
  • Français Object Oriented Programming in Python | Set 2 (hiding data and printing objects)
  • Español Object Oriented Programming in Python | Set 2 (hiding data and printing objects)
  • Türk Object Oriented Programming in Python | Set 2 (hiding data and printing objects)
  • Русский Object Oriented Programming in Python | Set 2 (hiding data and printing objects)
  • Português Object Oriented Programming in Python | Set 2 (hiding data and printing objects)
  • Polski Object Oriented Programming in Python | Set 2 (hiding data and printing objects)
  • Nederlandse Object Oriented Programming in Python | Set 2 (hiding data and printing objects)
  • 中文 Object Oriented Programming in Python | Set 2 (hiding data and printing objects)
  • 한국어 Object Oriented Programming in Python | Set 2 (hiding data and printing objects)
  • 日本語 Object Oriented Programming in Python | Set 2 (hiding data and printing objects)
  • हिन्दी Object Oriented Programming in Python | Set 2 (hiding data and printing objects)


  • Javier Robinson

    Shanghai | 2022-12-10

    मैं अपने कोडिंग साक्षात्कार की तैयारी कर रहा था, इसे स्पष्ट करने के लिए धन्यवाद - पायथन में Object Oriented Programming in Python | Set 2 (hiding data and printing objects) सबसे सरल नहीं है. कल चेक किया गया, यह काम करता है!

    Julia Innsbruck

    Paris | 2022-12-10

    Python तरीके और कार्य सब कुछ थोड़ा भ्रमित कर रहा है 😭 Object Oriented Programming in Python | Set 2 (hiding data and printing objects) एकमात्र समस्या नहीं है जिसका मुझे सामना करना पड़ा. प्रतिक्रिया के साथ कल वापस आऊंगा

    Dmitry Galleotti

    Massachussetts | 2022-12-10

    शायद कोई और जवाब हो? Object Oriented Programming in Python | Set 2 (hiding data and printing objects) का वास्तव में क्या अर्थ है?. मेरे स्नातक थीसिस में इसका इस्तेमाल करेंगे

    Shop

    Learn programming in R: courses

    $

    Best Python online courses for 2022

    $

    Best laptop for Fortnite

    $

    Best laptop for Excel

    $

    Best laptop for Solidworks

    $

    Best laptop for Roblox

    $

    Best computer for crypto mining

    $

    Best laptop for Sims 4

    $

    Latest questions

    NUMPYNUMPY

    Common xlabel/ylabel for matplotlib subplots

    12 answers

    NUMPYNUMPY

    How to specify multiple return types using type-hints

    12 answers

    NUMPYNUMPY

    Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

    12 answers

    NUMPYNUMPY

    Flake8: Ignore specific warning for entire file

    12 answers

    NUMPYNUMPY

    glob exclude pattern

    12 answers

    NUMPYNUMPY

    How to avoid HTTP error 429 (Too Many Requests) python

    12 answers

    NUMPYNUMPY

    Python CSV error: line contains NULL byte

    12 answers

    NUMPYNUMPY

    csv.Error: iterator should return strings, not bytes

    12 answers


    Wiki

    Python | How to copy data from one Excel sheet to another

    Common xlabel/ylabel for matplotlib subplots

    Check if one list is a subset of another in Python

    sin

    How to specify multiple return types using type-hints

    exp

    Printing words vertically in Python

    exp

    Python Extract words from a given string

    Cyclic redundancy check in Python

    Finding mean, median, mode in Python without libraries

    cos

    Python add suffix / add prefix to strings in a list

    Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

    Python - Move item to the end of the list

    Python - Print list vertically