【人工智能概述】python妙用 __str__()

【人工智能概述】python妙用 str()

文章目录


一.python内置函数__str__()

  • 通过自定义__str__()函数可以打印对象中相关的内容。
python 复制代码
class Person(object):
    def __init__(self, name = 'tom', age = 10):
        self.name = name
        self.age = age
        self.parent = None
    
    def __str__(self):
        return "Your name is: " + self.name
    
tom = Person()
print(tom)
 
# 唔,果然输出了我们自定义的内容
# >>> Your name is: tom
相关推荐
高级程序源7 分钟前
springboot本地生活服务平台33399-计算机课程设计、毕业设计
vue.js·spring boot·python·mysql·小程序·flask·课程设计
高级程序源12 分钟前
springboot社区旧物回收管理系统25381-计算机课程设计、毕业设计
vue.js·spring boot·python·django·课程设计
高级程序源14 分钟前
springboot大学生体测系统72170-计算机课程设计、毕业设计
java·vue.js·spring boot·后端·python·django·课程设计
谢亮_vipxieliang19 分钟前
JavaScript this 指向完全指南:调用位置决定一切
开发语言·javascript·ecmascript
denlaku24 分钟前
JDK 13 新特性详解
java·开发语言
打工仔折腾 AI27 分钟前
极空间用 Docker 部署 Photopea:网页版 PS 的私有化实践
运维·后端·python·docker·容器·langchain·ai agent 实战
Eiceblue35 分钟前
使用 Python 实现 Excel 行列转置(保留单元格样式)
前端·python·excel
念越36 分钟前
题库、练习与错题本代码阅读
开发语言·数据结构
WeiXin_DZbishe37 分钟前
基于Node.js的马拉松赛事管理系统的设计与实现-计算机毕设 附源码【课程设计】90211
java·javascript·vue.js·python·php·课程设计
l1t38 分钟前
几种数据库软件count(distinct)和count()from group by子查询性能测试
开发语言·数据库