简体   繁体   English

Python魔术函数,它如何工作?

[英]Python magic function, how does it work?

How does this magic function work (below) ie hex = '%032x' % self.int for some reason it turns the following value... 这个魔术函数如何工作(如下),即hex = '%032x' % self.int由于某种原因,它会变成以下值...

265631021230191344138857284998518456

into.... 进入...

0033289ed88646a64b9fc63c808fd6b8

def __str__(self):
    hex = '%032x' % self.int
    return hex

But all this should actually do from my knowledge is append 032x to the end of the string 265631021230191344138857284998518456 , so whats going on? 但是,据我所知,所有这些实际上应该做的就是将032x附加到字符串265631021230191344138857284998518456 ,这是怎么回事? How does it do what it does? 它是如何做的?

Full code here 完整代码在这里

%x is a format code for hex. %x是十六进制的格式代码 Your function is representing a decimal value in hex. 您的函数以十六进制表示十进制值。

>>> hex(265631021230191344138857284998518456)
'0x33289ed88646a64b9fc63c808fd6b8'

The full format code 完整格式代码

'%032x'

Means represent the value in hex and pad it with zeros on the left until it is 32 characters wide. 均值以hex表示值,并在左侧用零填充,直到其宽度为32个字符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM