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 9229631
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T05:37:12+00:00 2026-06-18T05:37:12+00:00

I have a function within a Python (2.7) class that should retrieve the values

  • 0

I have a function within a Python (2.7) class that should retrieve the values of the ‘cells’ around it in a 2 dimensional numpy array. If the index is out of range, I would the value should be set as None.

I’m struggling to find a way to do this without writing 8 try/catch statements, or without using multiple if x else None statements as is in my code below. While they would both work, they don’t seem very well structured, and I’m thinking there must be a simpler way to do this – I’m probably caught thinking about this in entirely the wrong way. Any help would be much appreciated.

# This will return a dictionary with the values of the surrounding points
def get_next_values(self, column, row):
    if not (column < self.COLUMNS and row < self.ROWS):
        print "Invalid row/column."
        return False

    nextHorizIsIndex = True if column < self.COLUMNS - 2 else False
    nextVertIsIndex = True if row < self.ROWS - 2 else False

    n = self.board[column, row-1] if column > 0 else None
    ne = self.board[column+1, row-1] if nextHorizIsIndex else None
    e = self.board[column+1, row] if nextHorizIsIndex else None
    se = self.board[column+1, row+1] if nextHorizIsIndex and nextVertIsIndex else None
    s = self.board[column, row+1] if nextVertIsIndex else None
    sw = self.board[column-1, row+1] if nextVertIsIndex else None
    w = self.board[column-1, row] if row > 0 else None
    nw = self.board[column-1, row-1] if 0 not in [row, column] else None

    # debug
    print n, ne, e, se, s, sw, w, nw
  • 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-06-18T05:37:14+00:00Added an answer on June 18, 2026 at 5:37 am

    Here is a standard trick: Create your board with an edge padded with the value None. Then you can access any inner 3×3 square and fill in the appropriate values with

    nw, n, ne, w, _, e, sw, s, se = (self.board[column-1:column+2, row-1:row+2]).ravel()
    

    For example,

    import numpy as np
    
    board = np.empty((10,10), dtype = 'object')
    board[:,:] = None
    board[1:9, 1:9] = np.arange(64).reshape(8,8)
    print(board)
    # [[None None None None None None None None None None]
    #  [None 0 1 2 3 4 5 6 7 None]
    #  [None 8 9 10 11 12 13 14 15 None]
    #  [None 16 17 18 19 20 21 22 23 None]
    #  [None 24 25 26 27 28 29 30 31 None]
    #  [None 32 33 34 35 36 37 38 39 None]
    #  [None 40 41 42 43 44 45 46 47 None]
    #  [None 48 49 50 51 52 53 54 55 None]
    #  [None 56 57 58 59 60 61 62 63 None]
    #  [None None None None None None None None None None]]
    
    column = 1
    row = 1
    nw, n, ne, w, _, e, sw, s, se = (board[column-1:column+2, row-1:row+2]).ravel()
    print(nw, n, ne, w, _, e, sw, s, se)
    # (None, None, None, None, 0, 1, None, 8, 9)
    

    Note that

    • when you define the board this way, the first non-None index is now 1, not 0.
    • I think it is more typical to think of the first index as the row,
      and the second index as the column, because when you print(board)
      that is the way the values are formatted. So perhaps you want board[row-1:row+2, column-1:column+2] instead. Of course, you could define your own print_board function, and then be free to use whatever convention you like.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In PHP, is it possible to have a function within a class that's non-static,
I have a program in python that includes a class that takes a function
I have a background worker which calls a function within a separate class. This
I have a function definition of the following form (within a large code): class
Background I have a function that takes a config object as an argument. Within
Lets say I have a recursive function that creates lists within lists. It return
I have a third-party GUI program that I'm wrapping with a Python class (using
How can I get the list of class functions from within __getattr__ function? Python
I have written a function in python that takes two arguments one of which
In Python, if I have a child function within a parent function, is the

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.