python - python - 返回对象函数/方法调用的类型
问题描述
我解决了这样一个问题,我遇到了一个问题。
下面是这句话的内容:使用命令“type”检查函数返回的对象的类型(print)和方法-hello类Person。
这是我到目前为止编写的代码:
class Person:
def __init__(self, name="John", surname="Douglas"):
self.name= name
self.surname= surname
def hello(self):
print("Hello" + self.name + " " + self.surname)
Person.hello= "text"
p = Person()
p.hello()
我想检查 Person 类的 Hello 方法返回的对象的类型。
使用以下命令:
print (type(p.hello()))
但是,这个命令什么也不返回——我不知道它是否能很好地完成这项工作。我想寻求帮助/建议。
我提前感谢您的每一个答案!
解决方案
我相信这就是您要实现的目标
class Person:
def __init__(self, name="John", surname="Douglas"):
self.name= name
self.surname= surname
def hello(self):
#return the string
return "Hello " + self.name + " " + self.surname
#Person.hello= "text"
#instantiate class Person outside the class
p = Person()
#p.hello()
print(type(p.hello()))
无论您在函数内部创建什么并希望在外部访问它,都必须返回它
推荐阅读
- opengl - 在 OpenGL 片段着色器中绘制三角形
- heroku - dyno 关闭时是否有任何 Heroku 请求队列用于存储请求?
- python - 获取用户输入以更改颜色的功能
- rust - 为什么从 /dev/zero 读取一行输入会使我的计算机无响应?
- rust - 如果我分配,为什么我得到“借用时临时价值下降”,但在通过函数传递时却没有?
- powershell - Powershell脚本通过将文件与文件夹名称匹配来将文件复制到文件夹
- pip - 没有名为 pymc3 的模块
- mono - 在 Webflux 中运行顺序操作
- c# - 如果选择了其他行,则 POST 数据模型为空 - MVC C#
- php - 注册后 Laravel 重定向到预期的 URL