Skip to main content
Code Review

Return to Answer

added 202 characters in body
Source Link
Graipher
  • 41.6k
  • 7
  • 70
  • 134

Even though return cuts the chain short if one matches, I would prefer to use an elif chain here, because it is more obvious, but YMMV (especially with automatic linters):

def stringify(value):
 """Returns the string representation of the value."""
 if value is None:
 return '-'
 elif value is True:
 return '✓'
 elif value is False:
 return '✗'
 return str(value)

Apart from that there is no real alternative, because all other approaches will fail on the difference between 0 and False, which you can only get with is or on unhashable types (like list).

The only other possibility I see is replacing the values for True, False, None after the conversion to str, unsing str.translate, but this would be unable to differentiate None and "None".

Even though return cuts the chain short if one matches, I would prefer to use an elif chain here, because it is more obvious, but YMMV (especially with automatic linters):

def stringify(value):
 """Returns the string representation of the value."""
 if value is None:
 return '-'
 elif value is True:
 return '✓'
 elif value is False:
 return '✗'
 return str(value)

Apart from that there is no real alternative, because all other approaches will fail on the difference between 0 and False, which you can only get with is or on unhashable types (like list).

Even though return cuts the chain short if one matches, I would prefer to use an elif chain here, because it is more obvious, but YMMV (especially with automatic linters):

def stringify(value):
 """Returns the string representation of the value."""
 if value is None:
 return '-'
 elif value is True:
 return '✓'
 elif value is False:
 return '✗'
 return str(value)

Apart from that there is no real alternative, because all other approaches will fail on the difference between 0 and False, which you can only get with is or on unhashable types (like list).

The only other possibility I see is replacing the values for True, False, None after the conversion to str, unsing str.translate, but this would be unable to differentiate None and "None".

added 37 characters in body
Source Link
Graipher
  • 41.6k
  • 7
  • 70
  • 134

Even though return cuts the chain short if one matches, I would prefer to use an elif chain here, because it is more obvious, but YMMV (especially with automatic linters):

def stringify(value):
 """Returns the string representation of the value."""
 if value is None:
 return '-'
 elif value is True:
 return '✓'
 elif value is False:
 return '✗'
 return str(value)

Apart from that there is no real alternative, because all other approaches will fail on the difference between 0 and False, which you can only get with is or on unhashable types (like list).

Even though return cuts the chain short if one matches, I would prefer to use an elif chain here, because it is more obvious, but YMMV (especially with automatic linters):

def stringify(value):
 """Returns the string representation of the value."""
 if value is None:
 return '-'
 elif value is True:
 return '✓'
 elif value is False:
 return '✗'
 return str(value)

Apart from that there is no real alternative, because all other approaches will fail on the difference between 0 and False, which you can only get with is.

Even though return cuts the chain short if one matches, I would prefer to use an elif chain here, because it is more obvious, but YMMV (especially with automatic linters):

def stringify(value):
 """Returns the string representation of the value."""
 if value is None:
 return '-'
 elif value is True:
 return '✓'
 elif value is False:
 return '✗'
 return str(value)

Apart from that there is no real alternative, because all other approaches will fail on the difference between 0 and False, which you can only get with is or on unhashable types (like list).

added 41 characters in body
Source Link
Graipher
  • 41.6k
  • 7
  • 70
  • 134

Even though return cuts the chain short if one matches, I would prefer to use an elif chain here, because it is more obvious, IMObut YMMV (especially with automatic linters):

def stringify(value):
 """Returns the string representation of the value."""
 if value is None:
 return '-'
 elif value is True:
 return '✓'
 elif value is False:
 return '✗'
 return str(value)

Apart from that there is no real alternative, because all other approaches will fail on the difference between 0 and False, which you can only get with is.

Even though return cuts the chain short if one matches, I would prefer to use an elif chain here, because it is more obvious, IMO:

def stringify(value):
 """Returns the string representation of the value."""
 if value is None:
 return '-'
 elif value is True:
 return '✓'
 elif value is False:
 return '✗'
 return str(value)

Apart from that there is no real alternative, because all other approaches will fail on the difference between 0 and False, which you can only get with is.

Even though return cuts the chain short if one matches, I would prefer to use an elif chain here, because it is more obvious, but YMMV (especially with automatic linters):

def stringify(value):
 """Returns the string representation of the value."""
 if value is None:
 return '-'
 elif value is True:
 return '✓'
 elif value is False:
 return '✗'
 return str(value)

Apart from that there is no real alternative, because all other approaches will fail on the difference between 0 and False, which you can only get with is.

Source Link
Graipher
  • 41.6k
  • 7
  • 70
  • 134
Loading
lang-py

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