473,383 Members | 1,919 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

array of class

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
14 3464
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
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

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
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
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
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
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: its me | last post by:
Let's say I have a class of people... Public Class People Public Sex as String Public Age as int Public Name as string end class And I declare an array of this class...
6
by: Buddy Ackerman | last post by:
I created a simple class: Public Class MyTestClass Public Test() As String End Class I tried to assign some values to the array Test() and display them like this:
14
by: Gianni Mariani | last post by:
Does anyone know if this is supposed to work ? template <unsigned N> int strn( const char str ) { return N; } #include <iostream>
3
by: michi | last post by:
Hello, I need to initialize a 2 dimensional square arrays of structures. The size of array I get from the user. I can do one-dimensional array, but I don't know how to specify the size of array...
9
by: justanotherguy63 | last post by:
Hi, I am designing an application where to preserve the hierachy and for code substitability, I need to pass an array of derived class object in place of an array of base class object. Since I...
7
by: Rade | last post by:
Please have a look at the following program: #include <iostream> template <const int array, size_t index> class ArrayIndex { public: static const int value = array; };
4
by: Armand | last post by:
Hi Guys, I have a set of array that I would like to clear and empty out. Since I am using "Array" not "ArrayList", I have been struggling in finding the solution which is a simple prob for those...
23
by: sandy | last post by:
I need (okay, I want) to make a dynamic array of my class 'Directory', within my class Directory (Can you already smell disaster?) Each Directory can have subdirectories so I thought to put these...
17
by: =?Utf-8?B?U2hhcm9u?= | last post by:
Hi Gurus, I need to transfer a jagged array of byte by reference to unmanaged function, The unmanaged code should changed the values of the array, and when the unmanaged function returns I need...
6
by: npankey | last post by:
I've started experimenting with template metaprogramming in a small project of mine. What I'm trying to accomplish is to generate a static array of templated objects that get specialized based on...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.