python-3.x - 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
解决方案
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()
推荐阅读
- css - 删除材质对话框上的奇怪边框
- c - 如何使这项工作适用于 3 个用户并通过而不是 1 C
- angular - 带有参数的特定路由的用户访问
- python - 通过硒,Python中的xpath检测用户可见元素(仅在视口中)
- c# - 如何让 C# 中的按钮上显示的文本?
- reactjs - 动作类型说明
- mysql-python - 如何在 Python 2.6 中使用 mysql.connector
- swift - 在 SpriteKit swift4 Xcode 中停止一个动作并设置一个定时器来切换玩家
- r - 根据列名和因子将数据从一个数据框收集到另一个数据框
- android - getUserConfirmation 不起作用反应本机