By Chandan Singh via Discussions on Python.org at 14Apr2022 00:19:
class RBI:
def Interest(self):
pass
class SBI(RBI):
def Interest(self):
print(“SBI FD interest rates are 5.1%”)
class PNB(RBI):
def Interest(self):
print(“PNB FD interest rates are 5.1%”)
S= SBI()
P= PNB()
print(S.Interest())
print(P.Interest())
OUTPUT:
SBI FD interest rates are 5.1%
None
PNB FD interest rates are 5.1%
None
I assume the above is at the interactive >>>
prompt?
The reason is that “print()” does not return a value from a function. It
just writes some text, usually to your screen.
The interactive prompt prints the value of the last expression you
typed. So:
>>> 3
3
A function with no return
statement still returns a value, it is just
the value None
.
So if your run looked like this:
print(S.Interest())
SBI FD interest rates are 5.1%
None
That is because the SBI.Interest
method printed “SBI FD interest
rates are 5.1%” and returned None
. You prompt want it to look like
this:
class SBI(RBI):
def Interest(self):
print("SBI FD interest rates are 5.1%")
return 5.1
or even more typically:
class SBI(RBI):
def Interest(self):
return 5.1
which you might use like this:
>>> interest = S.Interest()
>>> print(interest)
5.1
Actually, on reflection, the usual >>>
interactive prompt quietly
prints nothing if the return value is None
, so maybe you’re using
something else. But the situation is the same. Your function was not
returning a value, so it returned None
(the default).
Cheers,
Cameron Simpson cs@cskk.id.au