what is the python equivelant of this?

Mats Kindahl matkin at iar.se
Mon Oct 16 11:43:14 EDT 2000


"Alex Martelli" <aleaxit at yahoo.com> writes:

> "Mats Kindahl" <matkin at iar.se> wrote in message
> news:umd7h5q9k2.fsf at iar.se...
> > Neil Schemenauer <nas at arctrix.com> writes:
[snip]
>> I'm new to Python, but is there anything wrong with:
>>
>> recordarray = [0] * 10
> 
> Absolutely nothing wrong -- this makes 'recordarray' refer
> to a list of ten zeros (not to a list of ten "empty" things
> of "RECORD" type, which seems more similar to what was
> requested by the original poster).
>
> Assuming you have defined a "class Record", which, when
> called without arguments, returns an "empty" thing of the
> type we desire, then, there is a difference between:
> 
> recordarray = [Record()] * 10
> 
> this makes 'recordarray' refer to a list of ten references
> to ONE AND THE SAME "empty thing of Record type"; it's
> unlikely this is what one wants in this case...

I knew that! :)

Well, actually I'm just unused to programming in languages that have
reference semantics (e.g., Java, Python). I'm more used to value
semantics (e.g., C/C++, Perl, Lisp).

> recordarray = [Record() for i in range(10)]

Much nicer. Even readable. :)

-- 
Mats Kindahl, IAR Systems, Sweden

Any opinions expressed are my own, not my company's.



More information about the Python-list mailing list