我是python中的初学者...我写了这段代码,期望输出为,
global name:xxx
derived class
base class
globally declared function has been called
globally declared function has been called
但是我得到了以下输出,
Global name:xxx
derived class
derived class
base class
None
Globally declared function has been called
Globally declared function has been called
究竟是什么逻辑?我哪里出错?
这是我的代码:
Name=raw_input("Global name:")
def display():
print "Globally declared function has been called"
class base(object):
def __init__(self):
self.bname='base class'
print self.bname
def bfun1(self):
display()
class derived(base):
def __init__(self):
self.dname="derived class"
print self.dname
def bshow(self):
self.bsname=raw_input(super(derived,self).__init__())
print self.bsname
display()
ob=derived()
ob.__init__()
ob.bshow()
ob.bfun1()
答案 0 :(得分:2)
您不需要明确调用.__init__()
。在创建对象时已经调用它; ob = derived()
为您调用.__init__()
。
稍后,在bshow()
中,您明确地呼叫base.__init__()
,但包含在raw_input()
电话中。由于base.__init__()
没有明确的return
语句(它不应该有),raw_input()
会打印函数的{em>默认返回值,{{1} }。
以下代码将更接近您的预期:
None