类的数组 [英] array of class
问题描述
如何进行一个类数组?
s1 = [] ##此数组应该包含类
>
##班级定义
班级词语:
word =""
##空话...... INIT
for i in range(100):## 0..99
s1.append(Wort)
s1 [ 0] .word ="有"
s1 [1] .word =" should"
s1 [2] .word =" be"
s1 [3] .word =" different"
s1 [4] .word =" classes"
...但它不是。
打印s1
------------
[< class __main __。麦芽酒在0x7ff1492c>,
< class __main __。麦芽酒在0x7ff1492c>,
< class __main __。麦芽酒在0x7ff1492c>,
< class __main __。麦芽酒在0x7ff1492c>,
< class __main __。麦芽酒在0x7ff1492c>,
< class __main __。麦芽酒在0x7ff1492c>,
.........
-----------
这里,这个 C lasses"在记忆中都处于相同的位置。所以
在数组中没有不同的类。
所以我用s1 [0],s1 [1],s1 [2]等访问总是一样的数据。
有什么想法吗?
-
Michael
How can I do a array of class?
s1=[] ## this array should hold classes
## class definition
class Word:
word=""
## empty words... INIT
for i in range(100): ## 0..99
s1.append(Wort)
s1[0].word="There"
s1[1].word="should"
s1[2].word="be"
s1[3].word="different"
s1[4].word="classes"
.... but it''s not.
print s1
------------
[<class __main__.Wort at 0x7ff1492c>,
<class __main__.Wort at 0x7ff1492c>,
<class __main__.Wort at 0x7ff1492c>,
<class __main__.Wort at 0x7ff1492c>,
<class __main__.Wort at 0x7ff1492c>,
<class __main__.Wort at 0x7ff1492c>,
.........
-----------
Here, this "classes" are all at the same position in memory. So there
are no different classes in the array.
So I access with s1[0], s1[1], s1[2], etc. always the same data.
Any idea?
--
Michael
推荐答案
mm写道:
mm wrote:
>
怎么能我做了一个类的数组?
s1 = [] ##这个数组应该保存类
##类定义
class Word:
word =""
##空话... INIT
for我在范围(100):## 0..99
s1.append(Wort)
s1 [0] .word =" There"
s1 [1] .word =" should"
s1 [2] .word =" be"
s1 [3] .word =" different"
s1 [4] .word =" classes"
......但它不是。
print s1
------------
[< class __main __。麦芽酒在0x7ff1492c> ,<无线电通信/>
< class __main __。麦芽酒在0x7ff1492c>,
< class __main __。麦芽酒在0x7ff1492c>,
< class __main __。麦芽酒在0x7ff1492c> ;,
< class __main __。麦芽酒在0x7ff1492c>,
< class __main __。麦芽酒在0x7ff1492c>,
.... ....
-----------
这里,这个类在记忆中都处于相同的位置。所以
在数组中没有不同的类。
所以我用s1 [0],s1 [1],s1 [2]等访问总是相同的数据。
有什么想法吗?
-
Michael
>
How can I do a array of class?
s1=[] ## this array should hold classes
## class definition
class Word:
word=""
## empty words... INIT
for i in range(100): ## 0..99
s1.append(Wort)
s1[0].word="There"
s1[1].word="should"
s1[2].word="be"
s1[3].word="different"
s1[4].word="classes"
... but it''s not.
print s1
------------
[<class __main__.Wort at 0x7ff1492c>,
<class __main__.Wort at 0x7ff1492c>,
<class __main__.Wort at 0x7ff1492c>,
<class __main__.Wort at 0x7ff1492c>,
<class __main__.Wort at 0x7ff1492c>,
<class __main__.Wort at 0x7ff1492c>,
........
-----------
Here, this "classes" are all at the same position in memory. So there
are no different classes in the array.
So I access with s1[0], s1[1], s1[2], etc. always the same data.
Any idea?
--
Michael
你的意思是对象吗?
你的追加应该附加(Word()),因为你需要创建实例。
hg
do you mean object ?
your append should be append(Word()) as you need to create instances.
hg
mmaécrit:
mm a écrit :
>
如何进行类数组?
>
How can I do a array of class?
s / array / list /
s/array/list/
s1 = [] ##此数组应该包含类
##班级定义
班级词语:
word =""
##空话...... INIT
for i in range(100):## 0..99
s1.append(Wort)
s1=[] ## this array should hold classes
## class definition
class Word:
word=""
## empty words... INIT
for i in range(100): ## 0..99
s1.append(Wort)
我猜s / Wort / Word /
I guess that s/Wort/Word/
s1 [0] .word =" There"
s1 [1] .word =" should"
s1 [2] .word =" be"
s1 [3] .word = 不同
s1 [4] .word =" classes"
......但事实并非如此。
s1[0].word="There"
s1[1].word="should"
s1[2].word="be"
s1[3].word="different"
s1[4].word="classes"
... but it''s not.
错误...你确定你真的明白了什么是一个类是什么以及它是如何使用应该使用的
?
Err... Are you sure you really understand what''s a class is and how it''s
supposed to be used ?
>
print s1
------------
[< class __main __。Wort at 0x7ff1492c>,
< class __main __。Wort at 0x7ff1492c>,
< class __main __。Wort在0x7ff1492c>,
< class __main __。麦芽酒在0x7ff1492c>,
< class __main __。麦芽酒在0x7ff1492c>,
< class __main __。麦芽酒在0x7ff1492c>,
........
-----------
>
这里,这个类在记忆中都处于相同的位置。
>
print s1
------------
[<class __main__.Wort at 0x7ff1492c>,
<class __main__.Wort at 0x7ff1492c>,
<class __main__.Wort at 0x7ff1492c>,
<class __main__.Wort at 0x7ff1492c>,
<class __main__.Wort at 0x7ff1492c>,
<class __main__.Wort at 0x7ff1492c>,
........
-----------
Here, this "classes" are all at the same position in memory.
当然。您创建了一个包含100个对同一类的引用的列表。
Of course. You created a list of 100 references to the same class.
所以
在数组中没有不同的类。
So there
are no different classes in the array.
怎么可能?你什么时候在列表中放入另一个类?
How could it be ? When did you put another class in the list ?
所以我用s1 [0],s1 [1],s1 [2]等访问,总是一样的数据。
So I access with s1[0], s1[1], s1[2], etc. always the same data.
当然。
Of course.
有什么想法吗?
Any idea?
是的:阅读有关类和实例等OO基本概念的内容,
然后阅读Python的教程,了解这些概念是如何形成的用Python实现了
。
FWIW,我猜你想要的东西可能是这样的:
class Word(object):
def __init __(self,word =''''):
self._word = word
def __repr__ (个体经营):
返回"<%%s at%d>" %(self._word,id(self))
words = []
for w in [''this'',''is'',''可能'',''what'',''你',''想要'']:
words.append(Word(w))
打印文字
Yes : read something about OO base concepts like classes and instances,
then read the Python''s tutorial about how these concepts are implemented
in Python.
FWIW, I guess that what you want here may looks like this:
class Word(object):
def __init__(self, word=''''):
self._word = word
def __repr__(self):
return "<Word %s at %d>" % (self._word, id(self))
words = []
for w in [''this'', ''is'', ''probably'', ''what'', ''you'', ''want'']:
words.append(Word(w))
print words
mm写道:
mm wrote:
如何进行类数组?
s1 = [] ##这个数组应该包含类
##类定义
class Word:
word =""
##空话...... INIT
for i in range(100): ## 0..99
s1.append(Wort)
s1 [0] .word =" There"
s1 [1] .word =" should"
s1 [2] .word =" be"
s1 [3] .word =" different"
s1 [4] .word =" classes"
......但事实并非如此。
How can I do a array of class?
s1=[] ## this array should hold classes
## class definition
class Word:
word=""
## empty words... INIT
for i in range(100): ## 0..99
s1.append(Wort)
s1[0].word="There"
s1[1].word="should"
s1[2].word="be"
s1[3].word="different"
s1[4].word="classes"
... but it''s not.
我假设你想要一个对象(不是类)的列表(不是数组)。在
这种情况下,你在Word之后缺少括号。你必须调用
类对象,就像你调用一个函数一样,所以你必须遵循它
括号:
>
s1.append(Word())
事实上,你可以拥有一系列课程,实际上还有
的理由你可能想要这样做,但那是非常先进的。
Carl Banks
I presume you want an list (not array) of objects (not classes). In
that case, you''re missing parentheses after Word. You have to call the
class object, same as you''d call a function, so you have to follow it
with parentheses:
s1.append(Word())
You could, in fact, have an array of classes, and there are actually
reasons you might want to do that, but that''s pretty advanced.
Carl Banks
这篇关于类的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!