Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7551735
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T10:34:21+00:00 2026-05-30T10:34:21+00:00

This is a knowledge topic based on an earlier question I had today. These

  • 0

This is a knowledge topic based on an earlier question I had today. These are some odd inconsistencies in the behavior of numpy I’ve witnessed.

First, if you run this code:

A = ones((10,4))
view = A[:,1]
view.fill(7)
A

This will change the 2nd column to all 7s as arrays are indexed from 0 and slices are simply views of the same matrix. Awesome, that’s exactly what I want to happen.

Now, if you run this:

A = ones((10,4))
view = A[:,1:2]
view.fill(7)
A

It’ll have the same effects as the first example. Why is it that a:b specifies the columns from a to b-1? Is there a specific reason for this in the language? It seems that if I input say, 1:3, that should give me columns 1, 2, and 3 – not 1 and 2.

Finally, if you run this:

A = ones((10,4))
view = A[:,(1,2)]
view.fill(7)
A

There’s no side effects on A. It looks like if you create a view using a tuple, it somehow doesn’t correctly propagate any further side effects on the original matrix. Any insights?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T10:34:22+00:00Added an answer on May 30, 2026 at 10:34 am

    Why is it that a:b specifies the columns from a to b-1?

    This is a Python convention. The same is true for ordinary lists and range(a, b) will return a list containing the numbers a up to and including b-1, but not b. The benefit of this convention is that slicing by a:b where a and b are numbers will return b-a elements/rows/columns instead of the more complicated a-b+1.

    It looks like if you create a view using a tuple, it somehow doesn’t correctly propagate any further side effects on the original matrix.

    This is a Numpy idiosyncrasy, caused by the fact that it can only create views based on slices; these can be efficiently implemented, while tuple-based slices cannot. You can simulate this behavior with the following snippet, which shows what Python index syntax does under the hood:

    class FakeArray(object):
        def __getitem__(self, idx):
            return "You can't change the original FakeArray through me"
        def __setitem__(self, idx, val):
            print("We could set elements %r to %r here" % (idx, val))
    

    Now try

    >>> A = FakeArray()
    >>> A[1:2]
    "You can't change the original FakeArray through me"
    >>> A[1:2] = 'ham'
    We could set elements slice(1, 2, None) to 'ham'
    

    So A[1:2] is shorthand for A.__getitem__(slice(1, 2, None)) while A[1:2] = 'ham' is shorthand for A.__setitem__(slice(1, 2, None), 'ham'). Since there’s really two distinct methods involved, the behavior of slicing can be very different depending on whether it’s part of an assignment statement. In the Numpy case, there’s a subtle interplay between this difference and that between slice and tuple objects.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This question is just for a sake of knowledge. I don't have any practical
This has always bugged me. Perhaps someone with some hardcore knowledge of .NET internals
This is a followup to my earlier question about deciding if a hand is
Ok, this question is for people with either a deep knowledge of PRISM or
To my knowledge this doesn't break Liskov's Substitution Principle as an instance of a
I honestly have only started recently researching this so my knowledge is limited. I
This sounds simple but my regex knowledge is limited. I need an expression to
This is probably silly but I don't have enough Elisp knowledge to understand what
The purpose of this component is to test knowledge of a student on a
My knowledge of matlab is merely on a need to know basis, so this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.