Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #107472 > unrolled thread
| Started by | Stephen Hansen <me@ixokai.io> |
|---|---|
| First post | 2016-04-21 19:20 -0700 |
| Last post | 2016-04-21 19:20 -0700 |
| Articles | 1 — 1 participant |
Back to article view | Back to comp.lang.python
This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by
below is the oldest one visible, not the original post.
Re: How much sanity checking is required for function inputs? Stephen Hansen <me@ixokai.io> - 2016-04-21 19:20 -0700
| From | Stephen Hansen <me@ixokai.io> |
|---|---|
| Date | 2016-04-21 19:20 -0700 |
| Subject | Re: How much sanity checking is required for function inputs? |
| Message-ID | <mailman.2.1461291616.2861.python-list@python.org> |
On Thu, Apr 21, 2016, at 06:34 PM, Christopher Reimer wrote:
> class PieceFactory(object):
>
> def factory(color, piece, position):
> if piece == 'Bishop':
> return Bishop(color, position)
> if piece == 'King':
> return King(color, position)
> if piece == 'Knight':
> return Knight(color, position)
> if piece == 'Pawn':
> return Pawn(color, position)
> if piece == 'Queen':
> return Queen(color, position)
> if piece == 'Rook':
> return Rook(color, position)
This whole section is begging for a dictionary. Like...
_PIECE_TYPES= {"Bishop": Bishop, "King": King, ...]
class PieceFactory(object):
def factory(color, piece, position):
klass = __PIECE_TYPES.get(piece)
if klass is None:
raise PieceException("...")
return klass(color, position)
Or something like that.
That said, I'm not sure why its not just a function that does the same
thing. Why is it in a class that only does one thing? You never even
instantiate it.
> def generate_set(color, pieces, positions):
> for piece, position in zip(pieces, positions):
> yield getattr(PieceFactory, 'factory')(color, piece, position)
Whyyy are you using getattr? Something wrong with
PieceFactory.factory(color, piece, position)? (Or, better yet, yield
piece_factory(color, piece, position) where piece_factory is just a
function)
> I got the factory method from here:
> http://python-3-patterns-idioms-test.readthedocs.org/en/latest/Factory.html
I... that... what... I'd forget that link and pretend you never went
there. Its not helpful.
> Finally, VARS['VARIABLE_NAME'] got change to const['variable_name'].
> Should smell better.
I don't know that this changes anything about the small at all. What's
the contents of this big dictionary that has everything in it for some
reason?
That said, dear god, 'piece' doesn't look like an english word to me
anymore. I've never suffered semantic satiation from text before.
--Stephen
m e @ i x o k a i . i o
Back to top | Article view | comp.lang.python
csiph-web