array of class

H

hg

mm said:
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?


do you mean object ?

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

hg
 
M

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?
 
B

Bruno Desthuilliers

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>,
........

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
 
C

Carl Banks

mm said:
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
 
G

George Sakkis

Bruno said:
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
 
B

Bruno Desthuilliers

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 !-)
 
P

Podi

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.
 
H

hg

mm said:
Yes, it was the (), equivalent to thiks like new() create new object
from class xy.
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
 
H

hg

Neil said:
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.


And that is what I meant.

hg
 
M

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...
 
J

Jussi Salmela

hg kirjoitti:
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
 
N

Neil Cerutti

hg kirjoitti:

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.
 
B

Bjoern Schliessmann

mm said:
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
 
B

Bruno Desthuilliers

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.
 
B

Bruno Desthuilliers

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())

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).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top