wartime's recent timeline updates
wartime

wartime

V2EX member #29757, joined on 2012-11-20 09:37:10 +08:00
wartime's recent replies
os.chdir('G:\\123')
os.system('C:\\WINDOWS\\system32\\cmd.exe')
Apr 18, 2016
Replied to a topic by itlynn Python Python 的一个坑(元组)? or (多重赋值)?
@itlynn 对,而且 id((a,b))是取元组的 id ,和 a 、 b 的内存地址无关。
Apr 18, 2016
Replied to a topic by itlynn Python Python 的一个坑(元组)? or (多重赋值)?
id(...)
id(object) -> integer

Return the identity of an object. This is guaranteed to be unique among
simultaneously existing objects. (Hint: it's the object's memory address.)

(a, b) 实际上是临时分配的一个变量,由于没有引用可能马上释放。
(1, 2) 和 ('string', 2) 如果凑巧在相同位置,看上去 id 值一样,实际上之前的已经释放,内容已经改变。

c = (a,b) = (1,2)
print id(c)
d = (a,b) = ("string",2)
print id(d)

在 c 和 d 的值不变的情况下, id(c) id(d)值不变 (tuple 是 immutable)
Aug 27, 2015
Replied to a topic by hnsxyhh Python 为什么打印出来的长度不一样呢??
@hnsxyhh s 的起始地址是四字节对齐的,首先存放 int 类型值,就不需要再偏移, s1 里起始地址存放的是一个 char 类型,占用一个字节,假设是 s1 起始地址是 0x1000, int 存放地址就是从 0x1001 开始,没有对齐, 只有再偏移三个字节, 到 0x1004, 才能四字节对齐。
Aug 27, 2015
Replied to a topic by hnsxyhh Python 为什么打印出来的长度不一样呢??
int 类型在内存中存放地址 4 字节对齐, 导致 s1 占了额外空间.

s.pack (1, 'a')
'\x01\x00\x00\x00a'

s1.pack ('a', 1 )
'a\x00\x00\x00\x01\x00\x00\x00'

可以看到内存中实际存放方式
May 11, 2015
Replied to a topic by WKPlus Python 单循环两行代码打印出三角形
print reduce(lambda x, y: x * 10 + y, [i] * i)
Feb 20, 2015
Replied to a topic by sneezry Python Python 创建对象
p = type('P', (object,), {})()
p.x = 1
p.y = 2
p.v = type('P', (object,), {})()
p.v.x = 3
p.v.y = 4
Jan 27, 2015
Replied to a topic by ammzen Python Python 2.7 中字节字符串的处理求助
print ' '.join('{:02X}'.format(ord(i)) for i in s)
About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2548 Online   Highest 6679   ·     Select Language
创意工作者们的社区
World is powered by solitude
VERSION: 3.9.8.5 · 13ms · UTC 16:00 · PVG 00:00 · LAX 09:00 · JFK 12:00
♥ Do have faith in what you're doing.