Comprender la numerología de Python

We can not declare an integer which start with 0.

>>> n = 08
SyntaxError: invalid token

But we do declare a variable that contains all zeros.

>>> n = 00000
>>> print n
>>> 0

So the question is in first case why python just not consider value of variable to n = 8 by ignoring the zero on left side instead of raising an exception. As in second case it is still considering all zeros to a valid value.

Consider another case.

>>> n = '0008'
>>> print int(n)
>>> 8

Now in third case it is still considering it a valid numeric value, why an exception is not raised here??

preguntado el 01 de febrero de 12 a las 03:02

Funny, Python 2.7.1 allows n = 01 but Python 3.2 throws a SyntaxError. -

I was declaring variables on windows command line using python shell and it raises error there, while having this code in .py file does not produce the exception. Strange, I updated my question with output of shell. -

8 is not a valid digit in octal, if instead you put n = 010; print n. 8 will print out because 10 in octal is 8. -

4 Respuestas

Numbers beginning with 0 and containing no decimal point are interpreted as octal (using digits 0-7). 08 is not a valid octal number. According to the PEP Index, "the ability to specify an octal number by using a leading zero will be removed from the language in Python 3.0 (and the Python 3.0 preview mode of 2.6), and that a SyntaxError will be raised whenever a leading "0" is immediately followed by another digit" can be found here

Respondido 01 Feb 12, 07:02

so why int('0008') does not raise exception?? - Aamir Adnan

por la misma razón int('010') da 10. eval('08') will give you an exception though. - wim

It has to do with the number system. There's binary, hexadecimal, octal and etc.Take a look at number representation… - Frantz Romain

When it comes to explain different representations of numbers its better to see it visually because its utterly confusing and strange at first - Frantz Romain

En la mayoría de los idiomas, un 0 prefix indicates octal (base 8).

In python 2.7 I get:

>>> n = 010
>>> n

I suppose your problem is elsewhere. (If it's a version issue, though, I suppose they decided that almost no-one ever wants to hardcode a variable in base 8, so they made it an error).

- Editar:

Indeed, the octal literal format has changed (in 3.0):

Esto es ahora:

>>> n = 0o10
>>> n

(Works in 2.6+)

Respondido 01 Feb 12, 07:02

This is it. Python thinks it's octal. - remitente

so why int('0008') does not raise exception?? - Aamir Adnan

@AamirAdnan Well, you can probably safely assume that most of the time when trying to convert ints you prefer to have them be in base 10. So the default behaviour is to do that. Most languages have a way to parse a string as if it's in a certain base. But it probably just defaults to base 10. For example, consider: int('010', 8). - Seda del mediodía

The question about why the error happens has been answered by others; there was a convention for literales numéricos in Python, such that literals beginning with 0 were considered octal. If you put a digit larger than 7 in such a literal, the result was an error. Then, with Python 3, that convention was changed, and all literals with 0 at the beginning of them cause an error to be thrown.

The second question is why int doesn't throw that error. int doesn't throw that error because it has its own convention for specifying the base of a string: an optional second argument with a default value of 10. This allows int to accept a wider range of values without any concern for ambiguity. Consider this, for example:

>>> int('55', 16)
>>> int('0x55', 16)

The base here is specified; there's no ambiguity. It would be a handicap in this case to reject strings with the conventional 0x at the beginning. Likewise, it would be unreasonable to reject strings with 0 at the beginning when the base is unambiguously 10.

In general, it makes good sense (to me) for a language to be strict with literals, but more flexible with type conversions.

Respondido 01 Feb 12, 07:02

@Aamir Adnan, I realized that your question has a second part -- see above. - remitente

I got no problem with this:


def test2():
    n = 01
    print n


But for this it outputs 64(base 8):

def test2():
    n = 0100
    print n


Python 2.6.6

Respondido 01 Feb 12, 07:02

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.