In following python code, what is the difference between dict.setitem and setattr ? when I comment the setattr statement, I cannot run g.abcd. But if I keep the setattr statement, I can run both g.abcd and g['abcd'], which return the same value 300 id(g.abcd) and id(g['abcd']) returns the same value. I am confused: dict.setitem should insert a hash pair into the hash table. setattr should add an extra attributes (or field, or member) to class dict. so they seem to be different thing. Therefore I think it is expected that eliminating setattr will fail g.abcd. But I don't understand why id() returns the same value for g.abcd and g['abcd'], why they refer to the same thing? thx ================= class goc(dict): def __init__(self, name): dict.__init__(self) self._name = name def __setitem__(self, key, value): dict.__setitem__(self, key, value) # setattr(self, key, value) # g=goc() g.__setitem__('abcd', 300) =================
b*n
5 楼
Starwood Preferred Guest信用卡 开卡25000 bonus points 最高可入住12晚酒店 http://goo.gl/vJ71M Earn 10,000 Starpoints with your first purchase and an additional 15,000 Starpoints when you spend $5,000 in 6 months 可以直接兑换250刀amazon gift card http://goo.gl/vJ71M 填完后会在60秒告诉你结果,这个卡是第一年免年费的,之后是$65, 可以在第二年关 闭避免被收取年费。即使第二年开始有年费,个人觉得还是一张值得长期使用的信用卡 。 消费一笔后两周内会把10000 point在你卡上,不会使用的人可以兑换$100 giftcard. 选择其他的 giftcard(Starbucks, Gap,Nordstrom)请选择Redeem in other partners。请注意这一万点就可以换到最高5晚的category 1的酒店入住了。 下面的是非常关键的内容: 请大家尽快不要把点数兑换以上的250刀 amazon gift card,因为可以直接使用获得的 点数入住SPG酒店,Category 1: 以aloft为例,可以直接用2000points/晚 入住周末 房,或者以1200 points+25刀 在任意天入住。 Category 2: 以Four Points by Sheraton为例,可以使用3000points/晚 入住周末房, 或者以1600 points +30刀在任 意天入住。 即使是消费第一笔后获得的10000points,也可以获得最高五晚的酒店入住。而且这类 酒店在中国区也是有效的。如果消费满5000刀,更可以获得另外的最高七晚的酒店入住。
f*2
6 楼
不学习的话,吃完哪顿饭都没事。 要学习的话,早饭之后会饿,午饭,晚饭之后都会困。
t*d
7 楼
自己顶下。。。
P*1
8 楼
联邦税不管在哪个州。看你们有没有收到1042S。treaty只能用于学生(含OPT期间)up to 5000/人。 如果没有收到1042S,每人填一份8833说明自己的情况,两人的免税总额以负数形式填 入1040 Line 21
M*P
9 楼
setattr modifies g.__dict__ not the items in the dictionary. try g.__dict__ with or without setattr() and you will see the difference.
the to
【在 m*****e 的大作中提到】 : In following python code, what is the difference between dict.setitem and : setattr ? : when I comment the setattr statement, I cannot run g.abcd. But if I keep the : setattr statement, : I can run both g.abcd and g['abcd'], which return the same value 300 : id(g.abcd) and id(g['abcd']) returns the same value. : I am confused: dict.setitem should insert a hash pair into the : hash table. setattr should add an extra attributes (or field, or member) to : class dict. so they seem to be different thing. Therefore I think it is : expected that eliminating setattr will fail g.abcd. But I don't understand
thx, but why id() return the same value for both g[abcd] and g.abcd, which suggests they actually are the same thing (or place) in the memory
【在 M*P 的大作中提到】 : setattr modifies g.__dict__ not the items in the dictionary. : try g.__dict__ with or without setattr() and you will see the difference. : : the : to
could be that g['abcd'] is getting it's value from g.abcd or vice versa. as long as the program does what you want to do, why bother to know how it's done under the hood?
【在 m*****e 的大作中提到】 : thx, : but why id() return the same value for both g[abcd] and g.abcd, which : suggests they actually are the same thing (or place) in the memory
int is immutable: >> a, b = 12, 12 >> assert(id(a)==id(b)) 这是 python 基础,最好看看文档。 python容许不怎么懂的程序员也能勉强用, learning curve 低。不过一旦用多了就出问题,所以大家都是边用边学。
the to
【在 m*****e 的大作中提到】 : In following python code, what is the difference between dict.setitem and : setattr ? : when I comment the setattr statement, I cannot run g.abcd. But if I keep the : setattr statement, : I can run both g.abcd and g['abcd'], which return the same value 300 : id(g.abcd) and id(g['abcd']) returns the same value. : I am confused: dict.setitem should insert a hash pair into the : hash table. setattr should add an extra attributes (or field, or member) to : class dict. so they seem to be different thing. Therefore I think it is : expected that eliminating setattr will fail g.abcd. But I don't understand
In Python, every value is a pointer to an object. Assigning one variable to another copies the pointer so you have two pointers that point to the same object. Since they point to the same object, the id() will be the same. a = object() b = a # id(a) and id(b) are the same c = object() b = c # id(b) and id(c) are the same, # different from id(a) which is the same as before Just like in C++ for example: object *a = new object; // pretend we have a class called "object" object *b = a; // a and b are the same address object *c = new object; b = c; // b and c are the same address, // different from a which is the same as before
【在 m*****e 的大作中提到】 : thx, : but why id() return the same value for both g[abcd] and g.abcd, which : suggests they actually are the same thing (or place) in the memory
貌似是偶然跟别人讨论到的。前文“100以内”说得不准确,也没必要记, 设计思想就是常用的小整数缓存起来,避免频繁在堆上malloc造成不必要的开销。 刚翻了翻Python 3.4源码(注:Python 3里long已经和int统一了): http://hg.python.org/cpython/file/3.4/Objects/longobject.c #ifndef NSMALLPOSINTS #define NSMALLPOSINTS 257 #endif #ifndef NSMALLNEGINTS #define NSMALLNEGINTS 5 #endif /* Small integers are preallocated in this array so that they can be shared. The integers that are preallocated are those in the range -NSMALLNEGINTS (inclusive) to NSMALLPOSINTS (not inclusive). */ 前面说了,这个特性不是 *Python语言* 标准的一部分, 只是一个特定实现的特定优化手段,不要依赖这个特性做事。