首页 > 解决方案 > Why Doesn't Anything Return When Calling the Print() from a Class?

问题描述

I'm trying to learn about OOP concepts in Python and have this dumb little script which I saw from a YouTube video. This script should return "May name is Tom" however nothing returns when I execute it.

I'm sure i'm doing something very dumb, but can someone tell me why nothing prints out so that I can move forward and learn? I'm not finding any answer anywhere online.

i've tried searching online but to no avail

class Robot:
    def __init__(self, n, c, w):
        self.name = n
        self.color = c
        self.weight = w

    def introduce_self(self):
        print("My name is " + self.name)


r1 = Robot("Tom", "red", 30)

Absolutely nothing is displayed

标签: python-3.xoop

解决方案


When you do the following, you are creating an object of the class Robot.

r1 = Robot("Tom", "red", 30)

Unless you call the method of the class, the print statement wouldn't execute. So, after creating the class object, do the following to invoke the introduce_self() method.

r1.introduce_self()

推荐阅读