首页 > 编程语言 >Python - String representation of an instance object

Python - String representation of an instance object

时间:2024-07-30 20:52:26浏览次数:10  
标签:__ function string Python object instance str representation

The magic methods __str__ and __repr__ are used for converting an instance object into a string. The method __str__ is invoked when an instance object is converted to a string by calling the str built-in function. It is also invoked when an instance object is printed using the print function because print implicitly calls the str built-in function. The method __repr__ is invoked by the repr built-in function, and it is also used to display the object in the interactive terminal. If __str__ is not defined, then this method is invoked for str(obj) and print(obj) also.

Both __str__ and __repr__ methods return a string representation of the instance, but __str__ is generally used for the end user of the class; it returns a human-readable and user-friendly string representation of the object. The string returned by __repr__ is a descriptive and unambiguous string representation of the object. It returns a Python-interpretable text that can be used by programmers for debugging. This text is generally a valid Python expression from which you can re-create the instance object using the eval function.

 

Let us see what happens when we print an instance object of our Fraction class.

>>> f = Fraction(2, 3)

>>> print(f)

<__main__.Fraction object at 0x000001C9DB23B100>

We get a string containing the class name and the object id. The interactive echo and str function will also give the same string.

 

 

标签:__,function,string,Python,object,instance,str,representation
From: https://www.cnblogs.com/zhangzhihui/p/18333327

相关文章

  • 环境变量和python多版本共存,视图层源码分析,视图层总结,路由层,
    Ⅰ环境变量和python多版本共存【一】环境变量【1】什么是环境变量无论是win,mac,linux都有环境变量的概念,以win为例什么是环境变量?环境变量(environmentvariables)一般是指在操作系统中用来指定操作系统运行环境的一些参数,如:临时文件夹位置和系统文件夹位置等。环境变量是在......
  • Python面试题:如何使用Django Rest Framework构建RESTful API
    使用DjangoRestFramework(DRF)构建RESTfulAPI是一个常见且强大的方法。以下是一个详细的指南,帮助你从头开始创建一个基本的Django项目,并使用DRF构建一个RESTfulAPI。环境准备安装Django和DjangoRestFramework:pipinstalldjangodjangorestframewor......
  • python高性能计算:cython入门代码
    三种实现的对比:(1)纯pythonx.pydefis_prime(num):forjinrange(2,num):if(num%j)==0:returnFalsereturnTrueimporttimea=time.time()foriinrange(10,100000):is_prime(i)b=time.time()print(b-a)(2)x2.py......
  • 【第二节】python编程基础语法
    目录一、运算符介绍1.1算术运算符1.2比较运算符1.3赋值运算符1.4位运算符1.5逻辑运算符1.6成员运算符1.7身份运算符二、python运算符优先级三、三大流程结构四、列表五、元组六、字典一、运算符介绍1.1算术运算符1.2比较运算符1.3赋值运算符......
  • 基于python的百度迁徙迁入、迁出数据分析(四)
    这篇文章是对上篇文章的可获取数据的时间区间的修正,依然通过开发者模式找寻相关数据源,我直接把数据url贴在这里,可以发现里面包含了相对明面上看不到的数据包括,行政区id、春运迁徙数据等:qianxi.cdn.bcebos.com/app/index.js?9bf6150c2c2807aeaddb上篇文章在这里,有兴趣的可以连......
  • python高性能计算:cython使用openmp并行 —— 报错:undefined symbol: omp_get_thread_n
    test.pyx文件:fromcython.parallelcimportparallelfromopenmpcimportomp_get_thread_numcpdefvoidlong_running_task1()noexceptnogil:whileTrue:passcpdefvoidlong_running_task2()noexceptnogil:whileTrue:passdefdo......
  • 使用Python Paramiko创建文件目录并上传文件的终极指南
    哈喽,大家好,我是木头左!前言:为何选择Paramiko?在网络运维和自动化领域,SSH(SecureShell)协议是连接和管理远程服务器的常用手段。而Paramiko是一个用于进行SSH2会话的Python库,它支持加密、认证和文件传输等功能。使用Paramiko,可以方便地实现远程命令执行、文件上传下载等操作。准......
  • python_爬虫基础
    python爬虫基础1、初识爬虫1.rebots协议网站通过Robots协议告诉搜索引擎哪些页面可以抓取,哪些页面不能抓取,但它仅仅是互联网中的约定而已,可以不用遵守。例如:https://www.taobao.com/robots.txt2.谷歌浏览器插件●XPathHelper●WebScraper●ToggleJavaScript●User-......
  • Python - Creating Managed Attributes using properties
    CreatingManagedAttributesusingpropertiesPropertiescanbeusedtocreatedataattributeswithspecialfunctionality.Ifyouwantsomeextrafunctionality(liketypechecking,datavalidationortransformation)whilegettingorsettingadataattribut......
  • Python - Static Methods
    Sometimeswehavetowritemethodsthatarerelatedtotheclassbutdonotneedanyaccesstoinstanceorclassdataforperformingtheirwork.Thesemethodscouldbesomehelperorutilitymethodsthatareusedinsidetheclassbuttheycanperformthei......