0

If I am doing this

>>> 5.bit_length()

I am getting SyntaxError: invalid syntax

but runs fine when using space

>>> 5 .bit_length()
 Result -> 3

How does it reads the space ?

Carcigenicate
  • 43,494
  • 9
  • 68
  • 117
  • >>> a = 5 >>> a.bit_length() 3 This works, unclear about your use case of why a space is needed. – Avi Thaker Mar 08 '21 at 23:50
  • 2
    You can always add extra spaces between an expression and the `.` attribute access, e.g. `'asdssds' .lower() .upper()` the spaces are basically ignored. However, in the specific case of *numeric* literals, there is an ambiguity, because `5.` is a valid floating point literal, so `5.whatever` becomes a syntax error, because it's being parsed as float literal. A space suffices for this, hence `5 .whatever` is valid syntax, although a TypeError, but `5.whatever` is invalid syntax. More typically, you'd see parentheses, so `(5).bit_length()` – juanpa.arrivillaga Mar 08 '21 at 23:53
  • @AviThaker yes, but `5.bit_length()` is a syntax error. – juanpa.arrivillaga Mar 08 '21 at 23:55
  • Got the answer ! Thanks – Vehement_Py Mar 08 '21 at 23:58

0 Answers0