473,513 Members | 2,290 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Assignment to slice


I do not understand why python behaves the way it does
in the following code:

Here, Python doesn't like that I'm assigning
out-of-range. OK. That's fine.
x = []
x[5] = 3 Traceback (most recent call last):
File "<interactive input>", line 1, in ?
IndexError: list assignment index out of range

However, the following seems very strange. Assigning
to a slice, over non-existent array indexes, sets x[0]
and x[1]. x[2:5] = [3,4]
x [3,4] x[1000:9000] = [5]
x

[3, 4, 5]

Why does assigning to a slice of non-existent array
elements fill in the array, starting with the first
empty array position? (Acts like [].append() or
[].extend()?)

Why not raise an out-of-bounds exception here too?
Wouldn't that be more consistent with the first case,
in which I tried to assign out-of-range, and got an
exception?

IMO, at least the first index in the slice should be
required to exist in the array. If not, raise an
exception. That way, assigning to the slice fills in
the array in a predicatable way without having to do
len() checks first to ensure that you know where in
the array the slice is actually going to be inserted.

But I'm sure it is the way it is for a good reason.
Hopefully someone can clue me in.

Thank you in advance.

Rich


__________________________________
Do you Yahoo!?
Yahoo! Hotjobs: Enter the "Signing Bonus" Sweepstakes
http://hotjobs.sweepstakes.yahoo.com/signingbonus

Jul 18 '05 #1
0 1110

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
5463
by: Haoyu Zhang | last post by:
Dear Friends, Python assignment is a reference assignment. However, I really can't explain the difference in the following example. When the object is a list, the assignment seems to be a...
4
2625
by: Jens Lippmann | last post by:
Hi all! I'm new to Python and just tried to assign values to a portion of a string, but I don't get it. My code is: bits = '\3ff' * bmi.bmiHeader.biSizeImage ofs = 0x1E2C0 for i in range(0,...
11
2376
by: anongroupaccount | last post by:
What measures should be taken to avoid this sort of thing? class Base { }; class Derived1 : public Base { private: int i, j, k;
37
2737
by: Tim N. van der Leeuw | last post by:
Hi, The following might be documented somewhere, but it hit me unexpectedly and I couldn't exactly find this in the manual either. Problem is, that I cannot use augmented assignment operators...
35
2408
by: nagy | last post by:
I do the following. First create lists x,y,z. Then add an element to x using the augumented assignment operator. This causes all the other lists to be changed also. But if I use the assignment...
7
2202
by: Alexandre Guimond | last post by:
Hi all, i'm trying to deepcopy a slice object but i get the following error. Does anyone know a workaround? ActivePython 2.4.3 Build 12 (ActiveState Software Inc.) based on Python 2.4.3 (#69,...
2
7238
by: smichr | last post by:
It seems to me that the indices() method for slices is could be improved. Right now it gives back concrete indices for a range of length n. That is, it does not return any None values. Using an...
0
1323
by: Joey Mukherjee | last post by:
Hello! I am trying to use boost::multi_array and want to create a view where I am lowering the number of dimensions, but I wish the dimension to be reduced to be decided at runtime. I have a...
2
943
by: Andrew | last post by:
please explain this behavior to a newb:
0
7177
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7542
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5701
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5100
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
4756
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3248
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1611
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
811
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
470
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.