在python中作为输出的额外“None”

2024-09-30 16:35:57 发布

您现在位置:Python中文网/ 问答频道 /正文

class student:
    birth_day = 21 
    birth_month = 4 
    birth_year = 1998 
    def __init__(self,name):
        self.naav = name 
    def SayHi(self):
        return print('hello'+''+self.naav)


Topper = student('vikas')
print(Topper.naav) 
print(Topper.SayHi())
print(student.birth_day)  
print(Topper.birth_day)  
#print(student.naav)

这个的输出是

vikas
hellovikas
None
21
21

我对第三输出“无”感到困惑,不知道它是怎么工作的,有人帮我理解吗


Tags: nameselfinitdefyearstudentclassbirth
1条回答
网友
1楼 · 发布于 2024-09-30 16:35:57

这是因为在print(Topper.SayHi())上,您正在打印函数SayHi返回的内容。但是函数不返回任何内容(None)。 这是因为print('hello'+''+self.naav)不返回值,它做了一些事情,什么也不返回。 您应该做的是只返回字符串(更改我刚才提到的这一行),然后打印函数SayHi的返回(正如您已经在做的那样)。你知道吗

如果我不清楚或者你还不知道该怎么办,告诉我。你知道吗

相关问题 更多 >