Answer the question
In order to leave comments, you need to log in
Why does a class method output None?
Continuing to learn the basics of python, I ran into some problem
class schoolMember:
def __init__(self, name, age):
self.name = name
self.age = age
print('(Создан schoolMember {0})'.format(self.name))
def tell(self):
print('Имя {0}, возраст {1},'.format(self.name, self.age), end=" ")
return (self.name, self.age)
class Teacher(schoolMember):
def __init__(self, name, age, salary):
schoolMember.__init__(self, name, age)
self.salary = salary
print('Создан Teacher {0}'.format(self.name))
def tell(self):
schoolMember.tell(self)
print('Зарплата: {0}'.format(self.salary))
return
class Students:
def __init__(self, name, age, marks):
schoolMember.__init__(self, name, age)
self.marks = marks
print('Создан {0}'.format(self.name))
def tell(self):
schoolMember.tell(self)
print('Оценки {0}'.format(self.marks))
t = Teacher('Nina Petrovna', 50, 30000)
s = Students('Carl', 20, 80)
print()
members = [t,s]
for member in members:
print(member.tell())
Answer the question
In order to leave comments, you need to log in
but if you separately register for some tell object, but there is no None ( t.tell() for example )
t = Teacher('Nina Petrovna', 50, 30000)
t.tell()
t = Teacher('Nina Petrovna', 50, 30000)
print(t.tell())
I understand that there is no return in the method, and None comes out of this
tell()
returns None, you display this None and display it on the screen. You are clearly confused with print/return. For your implementation, you do not need to do print(obj.tell())
, because you call the necessary prints in the method itself. for member in members:
member.tell()
def tell(self):
return 'Имя {}, возраст {}'.format(self.name, self.age)
s = Students('Carl', 20, 80)
s.tell()
s = Students('Carl', 20, 80)
print(s.tell())
# либо
s = Students('Carl', 20, 80)
resutl = s.tell()
print(result)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question