Skip to main content
Code Review

Return to Answer

add namedtuple
Source Link
Dan Oberlam
  • 8k
  • 2
  • 33
  • 74

Inheriting from namedtuple makes this easier (thanks to Caridorc for pointing out in the comments).

from collections import namedtuple
class LogicalOperator(namedtuple('LogicalOperator', 'name symbol functor')):
 # no __init__ now
 # rest is unchanged

This way you've separated your concerns. The class encapsulates "what is a logical operator and how do I make it print pretty", and then we use it as necessary to get value and formatting for it.

This way you've separated your concerns. The class encapsulates "what is a logical operator and how do I make it print pretty", and then we use it as necessary to get value and formatting for it.

Inheriting from namedtuple makes this easier (thanks to Caridorc for pointing out in the comments).

from collections import namedtuple
class LogicalOperator(namedtuple('LogicalOperator', 'name symbol functor')):
 # no __init__ now
 # rest is unchanged

This way you've separated your concerns. The class encapsulates "what is a logical operator and how do I make it print pretty", and then we use it as necessary to get value and formatting for it.

Source Link
Dan Oberlam
  • 8k
  • 2
  • 33
  • 74

You should make a class. This lets you consolidate the implementation to be consistent, and simplifies a number of the details. You can also implement __str__ and __repr__ to make it easier to print them. Use string formatting for this

class LogicalOperator(object):
 def __init__(self, name, symbol, functor):
 self.name = name
 self.symbol = symbol
 self.functor = functor
 
 def __call__(self, a, b):
 return bool(functor(a, b))
 def __str__(self):
 return self.name
 def __repr__(self):
 return "{}, {}".format(self.name, self.symbol)

This way you've separated your concerns. The class encapsulates "what is a logical operator and how do I make it print pretty", and then we use it as necessary to get value and formatting for it.

Then we can still create all of our logical operators, but we do it with classes and a tuple instead of a dict and tuples.

ops = (
 LogicalOperator('AND', '∧', operator.and_),
 LogicalOperator('OR', '∨', operator.or_),
 LogicalOperator('NOT', '¬', lambda a, _: not a),
 LogicalOperator('NOR', '↓', lambda a, b: not (a or b)),
 LogicalOperator('XOR', '↮', operator.xor),
 LogicalOperator('XNOR', '↔', lambda a, b: not (a ^ b)),
 LogicalOperator('NAND', '↑', lambda a, b: not (a and b))
)

A better way to pad strings is to use string.rjust. An even better way is to use the Format Specification Mini-Language.

Then we can put it all together.

if __name__ == '__main__':
 column_width = 25
 table = list(itertools.product([False, True], repeat=2))
 for op in ops:
 print(op)
 print("-"*column_width)
 for left, right in table:
 print("|{:^5}|{:^5}|{:^5}|".format(left, right, op(left, right)))
 print("-"*column_width)

Also, you could use more whitespace throughout. Makes it easier to read.

lang-py

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