【人工智能概述】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
相关推荐
许彰午3 分钟前
87_Python Django模型与数据库
数据库·python·django
程序员小远13 分钟前
性能测试之性能调优
自动化测试·软件测试·python·测试工具·职场和发展·测试用例·性能测试
敖行客 Allthinker23 分钟前
IM 融合专题:后端架构师的核心修炼
java·开发语言·数据库
进击的程序猿~28 分钟前
Go Slice源码深度解析指南
开发语言·后端·golang
金銀銅鐵30 分钟前
[Python] 借助图形化界面探索模n运算的规律
python·数学
Hesionberger31 分钟前
快速求解完全平方数的最少数量
开发语言·数据结构·python·算法·leetcode·c#
xywww1685 小时前
大模型 API 选型实战:GPT、Gemini、Claude 接入时该看哪些指标?
运维·服务器·人工智能·python·gpt·langchain
夜雪一千10 小时前
Python enumerate() 函数完整详解:遍历同时获取索引,告别手动计数
服务器·windows·python
能有时光10 小时前
PyTorch KernelAgent 源码解读 ---(4)--- ExtractorAgent
人工智能·pytorch·python
_Jimmy_10 小时前
Python 协程库如何使用以及有哪些使用场景
python