Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python 2to3 tool adds a vowel to my integer

I was running the 2to3 tool on various scripts I'd written to get an idea of what I will need to change to port these to Python 3 (though I will be doing it by hand in the end).

While doing so, I ran into an odd change 2to3 made in one of my scripts:

-def open_pipe(pipe, perms=0644):
+def open_pipe(pipe, perms=0o644):

Um... Why did 2to3 add a "o" in the middle of my "perms" integer?

That's line 41 from the original source found here: https://github.com/ksoviero/Public/blob/master/tempus.py

like image 513
Soviero Avatar asked Jan 10 '23 10:01

Soviero


2 Answers

Try typing 0644 in your python2 shell. It will give you a different number because it is octal. In python3, the 0o signifies an octal number.

python2:

>>> 0644
420
>>> 

python3:

>>> 0644
  File "<stdin>", line 1
    0644
       ^
SyntaxError: invalid token
>>> 0o644
420
>>> 

New in python3:

Octal literals are no longer of the form 0720; use 0o720 instead.

like image 109
A.J. Uppal Avatar answered Jan 17 '23 18:01

A.J. Uppal


According to What's New In Python 3.0 - Integers:

Octal literals are no longer of the form 0720; use 0o720 instead.

like image 29
falsetru Avatar answered Jan 17 '23 17:01

falsetru