5 ms·
I was a bit bemused by this, sorry. eg. `(a += b) == a + b` is not true, a += b is not an expression. `str.is_digit()` doesn't 'check number', it checks if th
by sago 11y ago
I was a bit bemused by this, sorry. eg.
`(a += b) == a + b` is not true, a += b is not an expression.
`str.is_digit()` doesn't 'check number', it checks if the string is entirely composed of unicode digit codepoints.
`dict(col)` doesn't work for a general collection `dict([1,2,3])` for example.
Are these really the things people forget the most coming from another language? Not try/raise/except/finally? Negative indices?
If it works, it works, good on you for making it (and making it look good), but in general, it seems to be a bit insubstantial.
- nvader 11y agoI think this was an April Fool's Gag, although I'm not 100% clear on this. It certainly should be, if not :P
- aidos 11y agoI don't think that expression was meant to evaluate to True - they were just reminding themselves that you can increment by an amount using += in Python. This list of full of basic stuff though, and people who were referring to this would write insainly non-idiomatic Python, but hey, you gotta start somewhere. When I went to doing Python full time (after having built stuff in it on and off for years) it took me a good 6 months before I was able to write idiomatic Python. That's with over 15 years development experience and actively reading lots of nice Python code to try to reach the same level. Reading something like this reminds me of the gulf between, "can code in Python" and "writes nice code in Python".
- amelius 11y ago> (a += b) == a + b Solution: b == 0
- kusanagiblade 11y agoI do have to apologize for that piece of code... I wrote it down more of a reminder... I didn't check the syntax if it works. I guess not. It will be fixed.
- id 11y agoI don't even know why this is on the front page. It's incomplete, inaccurate and just very low quality. And it's strange that they would use this font for code.
- noobie 11y ago>`str.is_digit()` doesn't 'check number', it checks if the string is entirely composed of unicode digit codepoints. What do you mean?
- epidemian 11y agoI'm not really sure. Maybe that: >>> u'0١߂३৪੫૬୭௮౯'.isdigit() True So it's not equivalent to matching [0-9]+, which some people might expect. It's quite nice i think, because apparently int() supports Unicode numeric characters too (i didn't know this): >>> int(u'0١߂३৪੫૬୭௮౯') 123456789
- sago 11y agoRight - `isdigit` checks for membership in unicode class Nd. So the other decimal digits you quote work. Similarly `int()` supports the same values. `isdigit` doesn't support Nl or No class digits (not even ones corresponding to decimal digits, like Ⅳ). I don't know if it is possible to construct an example where `isdigit` would return True but `int` would fail. It wouldn't surprise me, either way, but at the least the failure case isn't obvious. The real problem is the converse `"1.23".isdigit()` returns False, where 1.23 is a number, so it doesn't 'check number'. And `int("-23")` works, but `"-23".isdigit()` is False. You could argue it 'checks non-negative integer'. I happened upon a stack exchange answer a while back, accepted, that failed because of this bug. In general the Python way to check if one thing can be converted into another is to try, and trap the error. def isnumber(text): try: float(text) except ValueError: return False else: return True
- kusanagiblade 11y agoI like the try/raise/except. Good one. Will add it in there.