5 ms·
In the "Dictionary as a Switch Statement" example, how come the @staticmethod decorator is on a different indent level than the method it wraps?
by RodericDay 10y ago
In the "Dictionary as a Switch Statement" example, how come the @staticmethod decorator is on a different indent level than the method it wraps?
- mikewhy 10y agoThe class attributes are at the same indentation level. Seems like code that wouldn't parse, which doesn't seem very Pythonic.
- mikeckennedy 10y agoWhoops! I updated the graphic. But "programming" in PowerPoint is super tough with the indentation. It always removes the extra spaces to be helpful. Thanks for pointing it out.
- Fuzzwah 10y agoShould: 'n', Moves.Nort Actually be: 'n', Moves.North ?
- mikeckennedy 10y agoThat's not how your spell Nort? ;) Yes, thanks, updated.
- avyfain 10y agoAlso, instantiating the dict on each call to the `parse` staticmethod is not very efficient. Why not create the dict as a static/class variable and place the method in a classmethod instead?
- mikeckennedy 10y agoYes, I think I mention that in the lesson. You have a trade off. 1. Instantiate the dict each time, make the code super readable and the choices right next to where they are used. 2. Make the code efficient, move the dict to a static / instance level variable away from the use case at the cost of clarity and maintainability. My slide chose clarity and I would recommend you write clear code over fast code until you profile it and see it's too slow. That was my motivation for the slide and demo.