|
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 gives us information about the objects with which we work. In C++, we can do this by adding a friend ostream & amp; operator
|
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):
p >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: p >
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
p>
Driver code
t
=
Test (
1234
,
5678
)
print
(t)
p >
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.