给定的五个函数之间有什么区别,它们返回相同的输出,具体为什么它们在某些时候没有返回?

时间:2017-11-02 00:00:36

标签: python-3.x

#a class is created on here
class Employee:
    raise_amount=1.04

    def __init__(self, first, last, pay):
            self.k=first
            self.p=last
            self.l=pay
            self.email=first+'.'+last+'@gmail.com'
            self.h=self.email

    def fullname(self):
            return ('{} {}'.format(self.k,self.p))

    def fullname1(self):
            return print('{} {}'.format(self.k,self.p))

    def fullname2(self):
            print('{} {}'.format(self.k,self.p))

    def apply_raise(self):
            self.l=self.l*self.raise_amount


emp1= Employee('Aditya', 'Shrivastava', 5000000)
print(emp1.fullname())
print(emp1.fullname1())
emp1.fullname1()
emp1.fullname2()
print(emp1.fullname2())

输出结果如下:

Aditya Shrivastava
Aditya Shrivastava

Aditya Shrivastava
Aditya Shrivastava
Aditya Shrivastava
    没有

1 个答案:

答案 0 :(得分:2)

看看这个例子:

class Employee:
    def none_funct(self):
      return print("hello")

emp1= Employee('Aditya', 'Shrivastava', 5000000)
print(emp1.none_funct())

打印:

hello
None

为什么呢?我认为,print是一个执行动作并返回None的函数,所以这里:

print(emp1.none_funct())

print内,您有emp1.none_funct()打印“hello”,但返回None,而外部print打印出无