4 ms·
Hi, nice job. A few unsolicited code tips that seem to come up often if I may: - `if <somestuff>: return True; else: return False` is always best spelled `ret
by JulianWasTaken 14y ago
Hi, nice job.
A few unsolicited code tips that seem to come up often if I may:
- `if <somestuff>: return True; else: return False` is always best spelled `return <somestuff>`
- You only need the global statement when you want to assign to a global, not access one
- `foo = d["bla"] if "bla" in d else None` is just `foo = d.get("bla")`
- You have mutable default args. They probably are not what you want. See http://docs.python-guide.org/en/latest/writing/gotchas/#mutable-default-arguments http://docs.python-guide.org/en/latest/writing/gotchas/#muta... for e.g. for an explanation.
- Lots of your dicts with enumerates to set indices should just probably be lists
If I get a chance later I'll try to throw together a quick pull request, but otherwise, thanks for sharing.
- rodwyer 14y agothanks for the feedback, i'm new to Python and will take them onboard & fix those points in a couple of days if you follow the repository
- afandian 14y agomutable default args is probably the thing that will bite you the hardest!
- StavrosK 14y agoI will agree with everything but the first one. I prefer functions that claim to return True or False to return precisely that, rather than Truthy/Falsy values. Otherwise, if I want to do "if func() is other_val" where "other_val" is True or False, it will fail.
- JulianWasTaken 14y agoYes, you should never do ‛foo is True either :). Also, here the point is that <some stuff> was a boolean expression already.
- StavrosK 14y agoWell, how else would you check if a return value matches a True/False variable? if <var> and <retval>? That's horrible!
- VBprogrammer 14y agoObviously its a matter of personal preference but PEP 8 (official Python style guide) disagrees with you.
- StavrosK 14y agoPEP 8 only mentions truth checks, it doesn't mention if functions' return values should be True or the truthy value. Personally, I find it very unclean to have your function return half a string, or some unrelated int or something similar when your function is called, e.g. "is_member()".
- VBprogrammer 14y agoAh, sorry think we are talking about different things. I thought you where complaining about: if [something]: VS if [something] is True: In the case I think you are talking about I would probably agree with you that returning some non-boolean is less clean, though I would probably just use the equality operator to force a boolean result: def foo(i): return i == 0
- StavrosK 14y agoYep, we agree exactly. I just don't like "return i", and would rather do "return i is True" or something.
- krallin 14y agoIn that case, you can always opt to return bool(return_value), which lets you return True or False without the ugly if / else : )
- StavrosK 14y agoIndeed, or just "return var == True".