草庐IT

print_str

全部标签

python - python dict str() 函数是否可靠地对键进行排序?

在python中,字典{1:1,2:2,3:3}和{3:3,2:2,1:1}产生"{1:1,2:2,3:3}"当str()'d?我能否依赖这种排序,或者至少依赖于包含相同键/值对的字典在通过str()函数时会生成相同的字符串这一事实? 最佳答案 您不能依赖这两个属性中的任何一个。字典转换为字符串时的顺序还取决于键/值对的插入顺序。只要对Python源代码有一点了解(观看PyCon2010中的TheMightyDictionary),或者通过反复试验,您可以轻松找到反例:>>>{1:1,9:9}{1:1,9:9}>>>{9:9,1:1

用于 __str__ 和方法解析顺序的 Python Mixin

我发现我用Python编写的许多类都包含一小组变量,我实际上希望在调用str()时看到这些变量,并且重写__str__(self)foreach相当麻烦。因此,我编写了以下mixin,classStrMixin(object):'''Automaticallygenerate__str__and__repr__'''def__str__(self):importtypesname=self.__class__.__name__+':'attrs=['{}={}'.format(k,v)for(k,v)inself.__dict__.items()]returnname+','.join

python : Revert to base __str__ behavior

如果没有__str__方法,我如何恢复到python使用的默认函数?classA:def__str__(self):return"Somethinguseless"classB(A):def__str__(self):returnsome_magic_base_function(self) 最佳答案 你可以使用object.__str__():classA:def__str__(self):return"Somethinguseless"classB(A):def__str__(self):returnobject.__str__(

python - 打印到 Python : redirect vs print's file argument vs write 中的文件

我有一堆print调用需要写入文件而不是stdout。(我根本不需要stdout。)我正在考虑三种方法。其中任何一个有什么优势(包括性能)吗?完全重定向,我看到了here:importsyssaveout=sys.stdoutfsock=open('out.log','w')sys.stdout=fsockprint(x)#andmanymoreprintcalls#laterifIeverneedit:#sys.stdout=saveout#fsock.close()在每个打印语句中重定向:fsock=open('out.log','w')print(x,file=fsock)#an

python - 'str' 对象没有属性 '__dict__'

我想在Python中将字典序列化为JSON。我有这个'str'objecthasnoattribute'dict'错误。这是我的代码...fromdjango.utilsimportsimplejsonclassPerson(object):a=""person1=Person()person1.a="111"person2=Person()person2.a="222"list={}list["first"]=person1list["second"]=person2s=simplejson.dumps([p.__dict__forpinlist])异常(exception)情况是;

Python 继承 : Concatenating with super __str__

我想将子类的__str__实现添加到基础实现中:classA:def__str__(self):return"this"classB(A):def__str__(self):returnsuper(B,self)+"+that"但是,这会产生类型错误:TypeError:unsupportedoperandtype(s)for+:'super'and'str'有没有办法让str(B())返回"this+that"? 最佳答案 你需要做super(B,self).__str__()。super指的是父类;您没有调用任何方法。

python - 为什么包含 'end=' 参数的 python print 语句在 while 循环中表现不同?

我在MacOSX上运行python版本2.7.3。考虑这段代码:from__future__importprint_functionimporttimex=0whilex如果我运行这个脚本,我会观察到预期的输出:数字0到4,每个数字都附加了一个\n字符数字。此外,每个数字都会在暂停一秒后显示。01234现在考虑这个代码块:from__future__importprint_functionimporttimex=0whilex输出符合我的预期,01234没有\n,但时间出乎意料。该过程不会在一秒钟的暂停后显示每个数字,而是等待四秒钟,然后显示所有五个数字。为什么print('strin

python - str() 在 Python 中会失败吗?

在Python中是否存在str()抛出异常的情况? 最佳答案 是的,自定义类可能会失败:>>>classC(object):...def__str__(self):...return'oops:'+oops...>>>c=C()>>>str(c)NameError:globalname'oops'isnotdefined对于某些内置类,它甚至可能会失败,例如unicode:>>>u=u'\xff'>>>s=str(u)UnicodeEncodeError:'ascii'codeccan'tencodecharacteru'\xff'

从print(‘andunderst‘[3:6] + ‘andunderst‘[6:10] + ‘andunderst‘[0:3])中也许你发现自己白敲了几年Python代码

今天我们来聊下python代码:print('andunderst'[3:6]+'andunderst'[6:10]+'andunderst'[0:3])看到这句代码,可能很多同学会说这不简单吗?而且是简单的不能再简单。但你真的理解它吗?今天我们就来说说它:1.从栈的层面2.从堆的层面3.从代码内部运行顺序下面图片你将看到整个代码的执行过程。我们来一起看看吧。上面的图是代码没运行的时候,下面我们点下调试,之后用F7来一步步的运行调试。上图中在界面左上角点下调试按钮:上图中我们发现这条代码初压到栈中了再F7一下,上图中我们发现代码要开始执行'andunderst'[3:6]+'andunders

python - etree.tostring() xml python 中的 pretty_print

我正在尝试使用pretty_print选项打印出xml文档。但这是一个错误TypeError:tostring()得到了一个意外的关键字参数'pretty_print'我是不是漏掉了什么?defCreateXML2():Date=etree.Element("Date",value=time.strftime(time_format,time.localtime()));UserNode=etree.SubElement(Date,"User");IDNode=etree.SubElement(UserNode,"ID");print(etree.tostring(Date,prett