Jumping around when assigning elements

Matthew Sims matt at killermookie.org
Mon Dec 15 21:57:19 EST 2003


"Francis Avila" <francisgavila at yahoo.com> wrote in message news:<vtsgvm4d0dk157 at corp.supernews.com>...
> Matthew Sims wrote in message
> <1e963607.0312151408.583221e6 at posting.google.com>...
> >Python Newbie here. This is my first time learning object-oriented
> >programming and trying to break out of the usual Korn/Perl/PHP style
> >of programming. Having some difficulty understand some items.
> >
> >For lists, I understand this:
> >C=["need","some","help"]
> >print C[1]
> >some
> >
> >But I can't seem to do this:
> >C[3]="here"
> >
> >I know about C.append("here") but this brings me to my question...
> >
> >Is there anyway to assign to an element that wasn't defined in the
> >beginning? Like if I wanted element 5 assigned but not element 4
> >without using "" or None?
> 
> I assume you tried 'insert'?
> >>> C=["need","some","help"]
> >>> C.insert(10, 'here')
> >>> C
> ['need', 'some', 'help', 'here']
> 
> Ah, well that didn't work.  Python has no (builtin) way to do what you want.
> The reason isn't technical, but that the list needs *something* in that
> intervening space, and Python isn't about to make rash presumptions about
> what you want there ("explicit is better than implicit").  In choosing
> between two behaviors for inserting to an index beyond the end of a list,
> appending was deemed less surprising, less violent, and less likely to
> introduce mysterious bugs than filling in the intermediate elements with
> who-knows-what.

First off...I still have a lot to learn. :)

> 
> I can't even think of why one would want lists to have that sort of
> behavior.  It seems to me that if we're assigning to arbitrary elements and
> we want the list to grow with it, that we should be using a different data
> structure, like a dictionary with integer keys.  The index of a list
> generally has no semantic correlation to the data contained therein--it just
> specifies order, and the data doesn't care about its own index.  If we want
> a semantic correlation, we make that correlation explicit by a dictionary's
> key-value pair.
> 
> If you want to do this sort of thing (implicit assignment to intermediate
> elements in list insertions), write a function to do it, or subclass list.
> You can even modify the slice behavior itself (if you go the subclass
> route), so that C[3] will silently act like perl instead of Python, but this
> is *quite* unPythonic. (Other Python people reading your code will be
> confused.)

My guess is that this is what makes OO programming different than
modular.

> 
> If this sort of answer surprises you (coming from perl, it might...), do an
> "import this" at an interactive prompt and read. :)
> 

The Zen of Python, by Tim Peters...heh

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

Here's how the program basically worked. It's for a Tape Library with
a robot arm in the middle. There are 40 slots and each is represented
as an element in the array. If a tape was in one of the slots, that
element # corresponds to the slot # and the tape ID would show in that
element. All other elements represented blank slots. This allowed me
to control the robot arm to the exact slot a tape resides since all I
had to do was look at any elements that had tape IDs assigned.

So far the only way I could get this to work in Python was to insert
"" to represent an empty slot. But Python counts "" when I want to see
how many tapes are currently taking up slots "len(SLOTS)". It would
always show 40. So I now know that I need to write code to tell Python
not to count "". Not too difficult.

I guess I'm realizing how I need to approach Python as my first OO.
I'm re-writing the script as sort of a training. The program works
flawlessly in Perl, I'm just doing it to learn. So far it has taken
less lines of code in Python to perform the same tasks in Perl...up
til this point. ;)

Anyways, I appreciate the responses. I have a better understand of
Python.

--Matt




More information about the Python-list mailing list