由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - python programming question
相关主题
How to initialize object in constructor?[合集] C++ question -- how to save objects
问个Python的问题弱问一下
a python questionreturn value of a python function...
请教一个python的概念问题问问开发ios的,有用C++来组织代码的么?
这个python code能否优化去掉file writeFMP 3.0 Mitbbs 首发 — 求建议求反馈
python simple question怎么检测c++ smart pointer的循环引用?
请教pythonobjects status snapshot怎么做
请教个JAVA的小问题python一问
相关话题的讨论汇总
话题: phc话题: print话题: object话题: python
进入Programming版参与讨论
1 (共1页)
m*****e
发帖数: 35
1
lass M(object):
def __init__ (self):
self.a=20
a=[M()] *10
print len(a)
for phc in (a):
print "[%d]"%phc.a
a[0].a=30
for phc in (a):
print "[%d]"%phc.a
============
I want to have an array of M object. but it seems that everytime I change a
value in a[0], all other elements are changed too? what is wrong? how to
correct it?
thank you
m******e
发帖数: 44
2
All elements in a refer to the same object or address, change:
a=[M()] *10
to
a=[M() for i in range(10)]

【在 m*****e 的大作中提到】
: lass M(object):
: def __init__ (self):
: self.a=20
: a=[M()] *10
: print len(a)
: for phc in (a):
: print "[%d]"%phc.a
: a[0].a=30
: for phc in (a):
: print "[%d]"%phc.a

m*****e
发帖数: 35
3


【在 m******e 的大作中提到】
: All elements in a refer to the same object or address, change:
: a=[M()] *10
: to
: a=[M() for i in range(10)]

1 (共1页)
进入Programming版参与讨论
相关主题
python一问这个python code能否优化去掉file write
C, C++, Java, C#, Objective-C,python simple question
You Don’t Know JS: this & Object Prototypes请教python
array如何get set?请教个JAVA的小问题
How to initialize object in constructor?[合集] C++ question -- how to save objects
问个Python的问题弱问一下
a python questionreturn value of a python function...
请教一个python的概念问题问问开发ios的,有用C++来组织代码的么?
相关话题的讨论汇总
话题: phc话题: print话题: object话题: python