By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,813 Members | 1,252 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,813 IT Pros & Developers. It's quick & easy.

array of class

P: n/a
mm

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

Jan 2 '07 #1
Share this Question
Share on Google+
14 Replies


P: n/a
hg
mm wrote:
>
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

do you mean object ?

your append should be append(Word()) as you need to create instances.

hg

Jan 2 '07 #2

P: n/a
mm a écrit :
>
How can I do a array of class?
s/array/list/
s1=[] ## this array should hold classes

## class definition
class Word:
word=""
## empty words... INIT
for i in range(100): ## 0..99
s1.append(Wort)
I guess that s/Wort/Word/
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 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.
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 ?
So I access with s1[0], s1[1], s1[2], etc. always the same data.
Of course.
Any idea?
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
Jan 2 '07 #3

P: n/a

mm wrote:
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.
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

Jan 2 '07 #4

P: n/a
Bruno Desthuilliers wrote:
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
Or more compactly:

words = [Word(w) for w in 'this is probably what you want'.split()]
print words

George

Jan 2 '07 #5

P: n/a
George Sakkis a écrit :
Bruno Desthuilliers wrote:
(snip)
>>words = []
for w in ['this', 'is', 'probably', 'what', 'you', 'want']:
words.append(Word(w))
print words

Or more compactly:

words = [Word(w) for w in 'this is probably what you want'.split()]
print words
I didn't want to introduce yet some more "confusing" stuff !-)
Jan 2 '07 #6

P: n/a
Or more compactly:

words = [Word(w) for w in 'this is probably what you want'.split()]
print words

I didn't want to introduce yet some more "confusing" stuff !-)
Indeed, the for loop is perfectly fine and totally readable. Let's save
the "confusing stuff" to the Perl folks.

Jan 3 '07 #7

P: n/a
mm

Yes, it was the (), equivalent to thiks like new() create new object
from class xy.
s1.append(Word)
s1.append(Word())

But I was looking for a "struct" equivalent like in c/c++.
And/or "union". I can't find it.

Maybe you know a source (URL) "Python for c/c++ programmers" or things
like that.
Yes, I konw whats an object is...
Jan 3 '07 #8

P: n/a
hg
mm wrote:
>
Yes, it was the (), equivalent to thiks like new() create new object
from class xy.
> s1.append(Word)
s1.append(Word())

But I was looking for a "struct" equivalent like in c/c++.
And/or "union". I can't find it.

Maybe you know a source (URL) "Python for c/c++ programmers" or things
like that.
Yes, I konw whats an object is...

A struct in C is unrelated to a struct in C++ as a struct in C++ _is_ a
class.
hg

Jan 3 '07 #9

P: n/a
hg kirjoitti:
mm wrote:
>Yes, it was the (), equivalent to thiks like new() create new object
from class xy.
>> s1.append(Word)
s1.append(Word())

But I was looking for a "struct" equivalent like in c/c++.
And/or "union". I can't find it.

Maybe you know a source (URL) "Python for c/c++ programmers" or things
like that.
Yes, I konw whats an object is...


A struct in C is unrelated to a struct in C++ as a struct in C++ _is_ a
class.
hg
What does your sentence mean, exactly? If I take a C file xyz.c
containing a struct definition S, say, rename it to be xyz.cpp and feed
it to a C++ compiler, the S sure remains a struct and the C++ compiler
has no difficulty in handling it as a struct, so ?!?

Cheers,
Jussi
Jan 3 '07 #10

P: n/a
On 2007-01-03, Jussi Salmela <ti*********@hotmail.comwrote:
hg kirjoitti:
>mm wrote:
>>Yes, it was the (), equivalent to thiks like new() create new object
from class xy.
s1.append(Word)
s1.append(Word())

But I was looking for a "struct" equivalent like in c/c++.
And/or "union". I can't find it.

Maybe you know a source (URL) "Python for c/c++ programmers" or things
like that.
Yes, I konw whats an object is...


A struct in C is unrelated to a struct in C++ as a struct in C++ _is_ a
class.
hg

What does your sentence mean, exactly? If I take a C file xyz.c
containing a struct definition S, say, rename it to be xyz.cpp
and feed it to a C++ compiler, the S sure remains a struct and
the C++ compiler has no difficulty in handling it as a struct,
so ?!?
That's true.

But it's also true that

struct foo {
int x, y;
};

is exactly equivalent to:

class foo {
public:
int x, y;
};

The only difference between struct and class in C++ is the
default access specification of its members.

--
Neil Cerutti
For those of you who have children and don't know it, we have a nursery
downstairs. --Church Bulletin Blooper
Jan 3 '07 #11

P: n/a
mm wrote:
But I was looking for a "struct" equivalent like in c/c++.
And/or "union". I can't find it.
class Honk(object):
pass

test = Honk()
test.spam = 4
test.eggs = "Yum"

Is it this what you're looking for?
Maybe you know a source (URL) "Python for c/c++ programmers" or
things like that.
Have a look at "Learning Python" by Mark Lutz and David Ascher. I
found it very helpful because they often refer to C/C++.

Regards,
Björn

--
BOFH excuse #344:

Network failure - call NBC

Jan 3 '07 #12

P: n/a
hg
Neil Cerutti wrote:
On 2007-01-03, Jussi Salmela <ti*********@hotmail.comwrote:
>hg kirjoitti:
>>mm wrote:

Yes, it was the (), equivalent to thiks like new() create new object
from class xy.
s1.append(Word)
s1.append(Word())

But I was looking for a "struct" equivalent like in c/c++.
And/or "union". I can't find it.

Maybe you know a source (URL) "Python for c/c++ programmers" or things
like that.
Yes, I konw whats an object is...
A struct in C is unrelated to a struct in C++ as a struct in C++ _is_ a
class.
hg

What does your sentence mean, exactly? If I take a C file xyz.c
containing a struct definition S, say, rename it to be xyz.cpp
and feed it to a C++ compiler, the S sure remains a struct and
the C++ compiler has no difficulty in handling it as a struct,
so ?!?

That's true.

But it's also true that

struct foo {
int x, y;
};

is exactly equivalent to:

class foo {
public:
int x, y;
};

The only difference between struct and class in C++ is the
default access specification of its members.

--
Neil Cerutti
For those of you who have children and don't know it, we have a nursery
downstairs. --Church Bulletin Blooper

And that is what I meant.

hg
Jan 3 '07 #13

P: n/a
Podi a écrit :
>>>Or more compactly:

words = [Word(w) for w in 'this is probably what you want'.split()]
print words

I didn't want to introduce yet some more "confusing" stuff !-)


Indeed, the for loop is perfectly fine and totally readable. Let's save
the "confusing stuff" to the Perl folks.
This is not what I meant. I do like list comprehensions and use them a
lot - and I don't find anything "perlish" here.
Jan 4 '07 #14

P: n/a
mm a écrit :
>
Yes, it was the (), equivalent to thiks like new() create new object
from class xy.
Yeps. In Python there's no 'new' operator. Instead, classes are
themselves 'callable' objects, acting as instance factory. It's very
handy since it let's you replace a class with a factory function (or
vice-versa) without impacting client code.
> s1.append(Word)

s1.append(Word())

But I was looking for a "struct" equivalent like in c/c++.
You can use a dict or a class.
And/or "union". I can't find it.
What is your real use case ? Direct translation from one language to
another usually leads to code that's both non-idiomatic and inefficient.
IOW, don't try to write C++ in Python.
Maybe you know a source (URL) "Python for c/c++ programmers" or things
like that.
Not really, but quite a lot of Python programmers come from the C++
world. FWIW, I'd say that the official Python tutorial should be enough
to get you started. Then, and if you're at ease with OO, you may want to
have a look at more advanced stuff like special methods and attributes,
decriptors and metaclasses (here again documented on python.org).
Jan 4 '07 #15

This discussion thread is closed

Replies have been disabled for this discussion.