In Python 2.7, why should I enclose "int" in brackets when I want to call a method on it?

in Python 2.7, why do I need to enclose int in brackets when I want to call a method on it?

 >>> 5.bit_length() SyntaxError: invalid syntax >>> (5).bit_length() 3 
+6
source share
2 answers

This is a parser.

When Python sees . , he begins to look for decimal numbers. Your decimal is b , so it fails.

If you execute (5).bit_length() , then Python will first analyze what is between () , and then look for the bit_length method.


If you try:

 5..zzz 

You will get the AttributeError that you expect. This will not work for integers: 5. is a float.

+7
source

Because 5.something will be parsed as a floating point number.

+4
source

Source: https://habr.com/ru/post/958630/


All Articles