Jumping around when assigning elements

Mike Rovner mike at nospam.com
Mon Dec 15 18:53:41 EST 2003


Francis Avila wrote:
> Matthew Sims wrote in message
>> of programming. Having some difficulty understand some items.
>>
>> I'm currently re-writing a Perl script into Python and with Perl I
>> was free to assign any element in the array without having to fill
>> in the previous elements. I can't seem to do that in Python...unless
>> I'm doing it wrong.
>
> Again, in general, Python has *no* implicit
> assignments/declarations/modifications/etc.  If you don't
> *explicitly* ask a list to fill in its intermediate elements, Python
> won't do it.  This is a conscious design decision, and a great part
> of what makes Python Python. For at least two senses of "great", IMO.

As were stated here many times it's not a good idea to mechanicaly
translate perl to python, they are quite different in approaches
despite similar syntax and capabilities.
Better understand initial script logic and write python from scratch.
In your case it might be list, dictionary or even none of them, so
it's highly dependent on the task at hand.

> I could never learn perl (despite many attempts), so I ask, what does
> perl do to the intermediate elements?  Is a perl array more like a
> dictionary with implicit integer keys (so intermediate keys simply
> don't exist), or does it fill in the intermediate elements with 0 or
> something like that?  In what perl idioms is this behavior useful?
> (I still mean to learn perl one of these days, for shell scripting.)

perl doesn't assigns them, they are 'undef', so it's more like python's dict

Mike








More information about the Python-list mailing list