Skip to main content
Code Review

Return to Answer

replaced http://codereview.stackexchange.com/ with https://codereview.stackexchange.com/
Source Link

You could also use a dict that contains tuples instead of lists, and get the same result, as suggested in comments by Graipher Graipher, like so:

You could also use a dict that contains tuples instead of lists, and get the same result, as suggested in comments by Graipher, like so:

You could also use a dict that contains tuples instead of lists, and get the same result, as suggested in comments by Graipher, like so:

Incorporate Graipher's suggestion about using a dict that contains tuples instead of a dict that contains lists.
Source Link
Thomas Ward
  • 2.5k
  • 1
  • 18
  • 30
DIRECTION_VECTORS = {
 'n': [0, 1],
 's': [0, -1],
 'e': [1, 0],
 'w': [-1, 0]
 }
def unit_vector_from_cardinal(cardinal: str) -> numpy.array:
 if cardinal in DIRECTION_VECTORS.keys():
 return numpy.array(DIRECTION_VECTORS[cardinal])
 else:
 raise ValueError

You could also use a dict that contains tuples instead of lists, and get the same result, as suggested in comments by Graipher , like so:

DIRECTION_VECTORS = {
 'n': (0, 1),
 's': (0, -1),
 'e': (1, 0),
 'w': (-1, 0)
 }
DIRECTION_VECTORS = {
 'n': [0, 1],
 's': [0, -1],
 'e': [1, 0],
 'w': [-1, 0]
 }
def unit_vector_from_cardinal(cardinal: str) -> numpy.array:
 if cardinal in DIRECTION_VECTORS.keys():
 return numpy.array(DIRECTION_VECTORS[cardinal])
 else:
 raise ValueError
DIRECTION_VECTORS = {
 'n': [0, 1],
 's': [0, -1],
 'e': [1, 0],
 'w': [-1, 0]
 }
def unit_vector_from_cardinal(cardinal: str) -> numpy.array:
 if cardinal in DIRECTION_VECTORS:
 return numpy.array(DIRECTION_VECTORS[cardinal])
 else:
 raise ValueError

You could also use a dict that contains tuples instead of lists, and get the same result, as suggested in comments by Graipher , like so:

DIRECTION_VECTORS = {
 'n': (0, 1),
 's': (0, -1),
 'e': (1, 0),
 'w': (-1, 0)
 }
get_initial_coordinates_list could be static.
Source Link
Thomas Ward
  • 2.5k
  • 1
  • 18
  • 30

get_initial_coordinates_list may be a static method

A static method only relies on the arguments passed to it. Your get_initial_coordinates_list function does not directly access any of the properties or variables assigned directly to the class itself (such as body_coordinates_list), and only uses the arguments and non-class-specific methods/functions, so we can actually make it a static method, and not have to provide a self argument (which means it doesn't need to have access to everything else in the class). At least, in its current form, it can be made into a static method:

@staticmethod
def get_initial_coordinates_list(x_pos: int, y_pos: int, facing: str) -> list:
 ...

Optional: Consider a better ValueError message

Optional: Consider a better ValueError message

get_initial_coordinates_list may be a static method

A static method only relies on the arguments passed to it. Your get_initial_coordinates_list function does not directly access any of the properties or variables assigned directly to the class itself (such as body_coordinates_list), and only uses the arguments and non-class-specific methods/functions, so we can actually make it a static method, and not have to provide a self argument (which means it doesn't need to have access to everything else in the class). At least, in its current form, it can be made into a static method:

@staticmethod
def get_initial_coordinates_list(x_pos: int, y_pos: int, facing: str) -> list:
 ...

Optional: Consider a better ValueError message

more grammar fixing, I have horrible writing skills today...
Source Link
Thomas Ward
  • 2.5k
  • 1
  • 18
  • 30
Loading
added 75 characters in body
Source Link
Thomas Ward
  • 2.5k
  • 1
  • 18
  • 30
Loading
Additional recommendations after posting.
Source Link
Thomas Ward
  • 2.5k
  • 1
  • 18
  • 30
Loading
grammar, fixed an incomplete thought
Source Link
Thomas Ward
  • 2.5k
  • 1
  • 18
  • 30
Loading
Source Link
Thomas Ward
  • 2.5k
  • 1
  • 18
  • 30
Loading
lang-py

AltStyle によって変換されたページ (->オリジナル) /