由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - a python question
相关主题
问个Python的问题python有快速loop over dict的方法吗?
请教一个python的概念问题怎么用python把这个data读进去,变成dict结构
怎样include一个函数写Python 的苦恼之一:有人当c用,有人当bash用,有人当FP用。当然也有人当python用
return value of a python function...python 水平如何提高
C 里面有办法永久改变一个指针的属性吗?怎么学习python的函数里面调用函数本身(函数递归)?
newbie python questionpython, 怎么能把 tree存到 memory了 ?
做web服务的语言Python pandas 是坑不?
Python中如何快速查询dict是否存在某keyHow to initialize object in constructor?
相关话题的讨论汇总
话题: setattr话题: same话题: object话题: value话题: __
进入Programming版参与讨论
1 (共1页)
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. 这个问题有文档吗?还是从实现的源码看来的?
1 (共1页)
进入Programming版参与讨论
相关主题
How to initialize object in constructor?C 里面有办法永久改变一个指针的属性吗?
python smtp 587 连不上gmail, socket error?newbie python question
python比java慢这么多呀做web服务的语言
Python and C/C++ QuestionPython中如何快速查询dict是否存在某key
问个Python的问题python有快速loop over dict的方法吗?
请教一个python的概念问题怎么用python把这个data读进去,变成dict结构
怎样include一个函数写Python 的苦恼之一:有人当c用,有人当bash用,有人当FP用。当然也有人当python用
return value of a python function...python 水平如何提高
相关话题的讨论汇总
话题: setattr话题: same话题: object话题: value话题: __