【人工智能概述】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
相关推荐
m0_7467523010 小时前
c++怎么利用std--variant处理多种二进制子协议包的自动分支解析【进阶】
jvm·数据库·python
m0_7349497916 小时前
MySQL如何配置定时清理过期备份文件_find命令与保留周期策略
jvm·数据库·python
t***54417 小时前
Clang 编译器在 Orwell Dev-C++ 中的局限性
开发语言·c++
m0_5145205717 小时前
MySQL索引优化后性能没提升_通过EXPLAIN查看索引命中率
jvm·数据库·python
H Journey17 小时前
Python 国内pip install 安装缓慢
python·pip·install 加速
oy_mail17 小时前
QoS质量配置
开发语言·智能路由器·php
oyzz12017 小时前
PHP操作redis
开发语言·redis·php
nashane18 小时前
HarmonyOS 6学习:网络能力变化监听与智能提示——告别流量偷跑,打造贴心网络感知应用
开发语言·php·harmony app
Polar__Star18 小时前
如何在 AWS Lambda 中正确使用临时凭证生成 S3 预签名 URL
jvm·数据库·python
凌波粒18 小时前
Java 8 “新”特性详解:Lambda、函数式接口、Stream、Optional 与方法引用
java·开发语言·idea