【人工智能概述】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
相关推荐
weixin_4407305028 分钟前
字符编码笔记-utf-8 、unicode、gbk,ascii编码格式
开发语言·笔记
励志不掉头发的内向程序员1 小时前
【从零写一个CAD 03】三个 double 值得单独一个类吗:把视图变换抽成 View
开发语言·c++·qt·学习·系统架构
PHP实战开发录2 小时前
PHP接口大整数为什么变了
开发语言·php
Yolanda_20222 小时前
15.dataloader的使用
python
临床数据科学和人工智能兴趣组5 小时前
在R语言中, 使用 as.factor() 函数转换数值型变量
开发语言·r语言
杨杨杨大侠8 小时前
Jev、Kev、Laya:决策模型怎么选,什么时候需要微调?
人工智能·python·agent
andyweike8 小时前
php笔记
开发语言·笔记·php
LOVE️YOU9 小时前
Python 在定义函数时,究竟定义的是什么?
python
段一凡-华北理工大学9 小时前
大模型应用开发 100 天:Python + LLM 从入门到精通 day26~Prompt 调试与优化——A/B 测试与效果评估
windows·python·大模型·prompt·智能体·提示词工程·高炉智能化
Minecraft红客9 小时前
以撒的结合
python·游戏·电脑·娱乐