超级函数返回None以及结果

时间:2017-09-05 09:40:46

标签: python subclass python-2.x

我有以下代码与父类Result ---------------------------------------------------------------------------------- size: 39, sole: Leather , part1: black, part2: black, part3: black, part4: black size: 39, sole: Leather , part1: tan, part2: black, part3: black, part4: black size: 39, sole: Leather , part1: espresso, part2: black, part3: black, part4: black size: 39, sole: Leather , part1: grey, part2: black, part3: black, part4: black size: 39, sole: Leather , part1: navy, part2: black, part3: black, part4: black size: 39, sole: Leather , part1: red, part2: black, part3: black, part4: black size: 39, sole: Leather , part1: black, part2: tan, part3: black, part4: black size: 39, sole: Leather , part1: tan, part2: tan, part3: black, part4: black size: 39, sole: Leather , part1: espresso, part2: tan, part3: black, part4: black size: 39, sole: Leather , part1: grey, part2: tan, part3: black, part4: black size: 39, sole: Leather , part1: navy, part2: tan, part3: black, part4: black ..................... .....................and so on 和子类Test。 从外面,我试图访问父类的方法而不是子类。所以我期待父类的MyTest功能。 所以,我使用display函数来实现这一目标。到现在为止还挺好。但是当我尝试将超级函数的返回值分配给变量说super时,我看到它打印出我期待的内容并打印z

'None'

我试图理解为什么超级功能正在返回'无'以及预期值

2 个答案:

答案 0 :(得分:1)

python中没有明确使用find()的任何可调用默认会返回public boolean find(int x,int Arr[]){ for (int y=0;y<Arr.length;y++) { if(Arr[y]==x) { System.out.println(" Found , At Position : " + y); return true; } } System.out.println(" Not Found"); return false; } public void delete (int x,int Arr[]){ for (int y :Arr) { if(Arr[y]==x) { for (int n=y;n<x;n++) { Arr[n]=Arr[n+1]; System.out.println("Deleted."); } x--; } } } public void insert(int x,int Arr[]){ int temp[]=new int[Arr.length + 1]; for (int y=0;y<Arr.length;y++) { temp[y]=Arr[y]; } temp[Arr.length]=x; Arr=temp; }

所以,要解决这个问题,只需使用return而不仅仅是打印:

None

答案 1 :(得分:1)

您的MyTest.display方法不包含任何return语句。 因此,隐含return None。 因此,z = super(type(a), a).display(10)会将None分配到z

您需要在方法中附加return语句,例如:

def display(self, x):
    self.x = x
    print "Getting called form MyTest"
    return self.x