1

I read special method lookup

In the example, it is:

>>> 1 .__hash__() == hash(1)

I understand in this context '1' is an instance of integer. I guess the space after '1' and before '.' to make the interpreter know that '1' is an instance, not something else, but I am not sure. so, I just want to have some confirmation. Could someone explain the reason of this required space after '1'?

Edit: just to clarify, I test this in interactive python prompt

Andy L.
  • 24,909
  • 4
  • 17
  • 29
  • 3
    Otherwise it'd be a float. – Martijn Pieters Mar 18 '19 at 16:16
  • 2
    `1.__hash__()` is not a valid floating point number, which is what it's going to be parsed as due to the decimal point. I personally prefer writing it as `(1).__hash__` which I think is clearer. – kindall Mar 18 '19 at 16:17
  • @MartijnPieters: in Interactive promt, `1 .__hash__()` returns `1`. No error. But, `1.__hash__()` returns `SyntaxError: invalid syntax` – Andy L. Mar 18 '19 at 16:26
  • 1
    @kindall: `1.__hash__()` is a valid float number followed by an invalid, unexpected token instead of the expected decimal digits or another valid token for the current top of the stack tracking an incomplete expression or statement. – Martijn Pieters Mar 18 '19 at 16:31
  • @MartijnPieters: reading John Kugelman link, I now understanded what you are saying. Thanks both. – Andy L. Mar 18 '19 at 16:36
  • @andy: actually, John linked to a different page, I updated the duplicate link since. See the post edit history. – Martijn Pieters Mar 18 '19 at 16:54
  • @MartijnPieters: Oh, I see. The history shows the 2 links from you which explain directly on the issue while John links different page. Thanks for your links. It really helps clearing my head. – Andy L. Mar 18 '19 at 17:10
  • @anky_91: you comment about this question https://stackoverflow.com/questions/56311067/comparison-with-the-previous-line-and-summation/56311191#56311191, isn't it? On that question, I groupby 'df.X1.ne(df.X1.shift()).cumsum()'. Not by `X1`. So, in case of `1,1,1,2,2,1,1`, 'df.X1.ne(df.X1.shift()).cumsum()' will separate first 3 of '1' and last 2 of '1' into 2 different groups. Thefore, the calculation is still correct. – Andy L. May 26 '19 at 06:57

0 Answers0