m*****e 发帖数: 35 | 1 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) # <------- dict.setitem
setattr(self, key, value) # <-------- setattr
g=goc()
g.__setitem__('abcd', 300)
================= | M*P 发帖数: 6456 | 2 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
| m*****e 发帖数: 35 | 3 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
| M*P 发帖数: 6456 | 4 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
| r****t 发帖数: 10904 | 5 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
| f*******n 发帖数: 12623 | 6 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
| f*******n 发帖数: 12623 | 7 What does immutable have to do with it?
>>> a = 500
>>> b = 500
>>> id(a) == id(b)
False
【在 r****t 的大作中提到】 : int is immutable: : >> a, b = 12, 12 : >> assert(id(a)==id(b)) : 这是 python 基础,最好看看文档。 python容许不怎么懂的程序员也能勉强用, : learning curve 低。不过一旦用多了就出问题,所以大家都是边用边学。 : : the : to
| V*********r 发帖数: 666 | 8 CPython这个参考实现里,100以内的整数被缓存,所以id一样。
这个是implementation-specific的,不是Python语言的一部分,不可依赖。
【在 f*******n 的大作中提到】 : What does immutable have to do with it? : >>> a = 500 : >>> b = 500 : >>> id(a) == id(b) : False
| r****t 发帖数: 10904 | 9 nice. learned something new. 这个问题有文档吗?还是从实现的源码看来的?
【在 V*********r 的大作中提到】 : CPython这个参考实现里,100以内的整数被缓存,所以id一样。 : 这个是implementation-specific的,不是Python语言的一部分,不可依赖。
| V*********r 发帖数: 666 | 10
貌似是偶然跟别人讨论到的。前文“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语言* 标准的一部分,
只是一个特定实现的特定优化手段,不要依赖这个特性做事。
【在 r****t 的大作中提到】 : nice. learned something new. 这个问题有文档吗?还是从实现的源码看来的?
|
|