A
A
Adolf452021-06-23 21:54:00
Python
Adolf45, 2021-06-23 21:54:00

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())


If I understand correctly, then we created a members list in order to show all available objects using a loop. The problem itself is that None is present in the output. I understand that there is no return in the method, and None comes out of this, but if you separately register tell for some object, but there is no None (t.tell() for example). It is quite possible to simply write a tell for each object and the problem is solved, but this is not entirely good, because there can be much more objects, then it will be inconvenient to write for each, so I want to figure out how to do this using a list and a loop, but without None to after all. Hope for understanding and help

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
soremix, 2021-06-23
@Adolf45

but if you separately register for some tell object, but there is no None ( t.tell() for example )

If I understand correctly, then you mean that if you execute:
t = Teacher('Nina Petrovna', 50, 30000)
t.tell()

then None will not be output?
None appears because you are calling print, so if you write
t = Teacher('Nina Petrovna', 50, 30000)
print(t.tell())

you will see None in the same way.
I understand that there is no return in the method, and None comes out of this

Everything is correct, it 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.
So it would be better to write code like this:
for member in members:
    member.tell()

Wrapping your method in print() makes sense only when you are not printing text in the method itself, but only preparing the correct sentence and returning it with return.
def tell(self):
    return 'Имя {}, возраст {}'.format(self.name, self.age)

When calling such a method, you will not get any output on the screen:
s = Students('Carl', 20, 80)
s.tell()

But on the other hand, you can save the result of the work to a variable and use it later, or send it directly to print():
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 question

Ask a Question

731 491 924 answers to any question