H
H
Hikikomori912015-10-10 15:30:04
Python
Hikikomori91, 2015-10-10 15:30:04

OOP. Why is none displayed?

class Human:
    def __init__(self, head, body, legs):
        self.head = head
        self.body = body
        self.legs = legs

    def ChangePartHead(self, head, scrap):
        scrap.head = head


class Robot:
    head = "MetalHead"
    body = "MetalBody"
    legs = "MetalLegs"

    def ChangePartLegs(self, legs, meat):
        meat.legs = legs

obj1 = Human('FleshHead', 'FleshBody', 'FleshLegs')
obj = Robot()
print(obj1.ChangePartHead(obj1.head, obj))
print(obj.ChangePartLegs(obj.legs, obj1))

As planned, the robot should have a human head, and a human should have robot legs. Displays non, I can not understand because of what.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
C
Cobalt the Terrible, 2015-10-10
@Hikikomori91

print(obj1.ChangePartHead(obj1.head, obj))
Prints what is returned by the ChangePartHead method of the obj1 object. The specified method does not return anything, which is why None is output.

V
Vov Vov, 2015-10-12
@balamut108

To correctly print, use the magic methods __repr__ and __str__, also read this article: habrahabr.ru/sandbox/82471

D
Dmitry, 2015-10-10
@EvilsInterrupt

/offtop: I recommend using the __slots__ keyword. This will reduce the size of the class object.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question