Facebook Twitter Instagram
    Facebook Twitter Instagram Pinterest Vimeo
    Hand On CodeHand On Code
    Hand On CodeHand On Code
    Home»Feature»Object Oriented Programming in Python Set 2 Data Hiding and Object Printing
    Feature

    Object Oriented Programming in Python Set 2 Data Hiding and Object Printing

    March 21, 2023Updated:March 22, 2023No Comments3 Mins Read
    Share
    Facebook Twitter LinkedIn Pinterest Email

    Part 2 of the Python Object-Oriented Programming Series (Data Hiding and Object Printing) Courses, Exercises, and Reading/Discussion Videos

    Object-Oriented Programming in Python, Set 1 is required prior to taking this course (Class, Object and Members) The Disguise of Data

    Python’s double-underscore (or __) notation makes attribute names invisible to the naked eye.

    outside.

    • Python

    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' 

    The preceding code raised an error because it attempted to access a protected variable outside of its class by means of an object. Using a sneaky method, we may determine the true value of a previously concealed characteristic.

    syntax:

    • Python

    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

    Although not directly available from outside the class, private methods may still be used. There is no such thing as true privacy in Python; secret methods and attributes have their names dynamically mangled and unmangled to make them seem inaccessible by their supplied names [See here for source]. Object Printing

    Information about the items we’re dealing with may be gained via printing them. Adding a buddy ostream& operator (ostream&, const Foobar&) function to the class in C++ does the same thing. the function toString() { [native code] }() function is used in Java. This may be done using __repr__ or __str__ in Python.

    methods.

    • Python

    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]

    Crucial Facts About Printing:

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

    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

    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> 
    Learn Python free Object Oriented Programming in Python Set 2 Data Hiding and Object Printing Python Code Python Course Free download python coursefree Courses Download Python Language
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email
    Previous ArticlePython Modules and Packages An Introduction
    Next Article CollectionsUserString in Python

    Related Posts

    python

    Class method vs Static method in Python

    April 7, 2023
    python

    Python Program to Count the Number of Matching Characters in a Pair of String

    April 7, 2023
    python

    Coroutine in Python

    April 7, 2023
    Add A Comment

    Leave A Reply Cancel Reply

    Facebook Twitter Instagram Pinterest
    © 2023 ThemeSphere. Designed by ThemeSphere.

    Type above and press Enter to search. Press Esc to cancel.