Is string formatting with % still allowed in Python 3.x? [duplicate] - python

This question already has answers here:
Python's many ways of string formatting — are the older ones (going to be) deprecated?
(5 answers)
Closed 9 years ago.
I read here that The plan is to eventually make this ["".format()] the only API for string formatting, and to start deprecating the % operator in Python 3.1.
I tried the % syntax with Python 3.1, 3.2 and 3.3, and it's working. So is there still a plan to remove the % syntax from Python in a future version, or can I use it freely?

This is not a definitive answer, but it is too large to make the point in a comment. The change in documentation wording in subsequent versions definitely moves away from stating the % syntax is deprecated.
From Old String Formatting Operations in v3.0 and Old String Formatting Operations in v3.1:
The formatting operations described here are obsolete and may go away in future versions of Python.
From Old String Formatting Operations in v3.2:
However, there are no current plans to deprecate printf-style formatting.
Old String Formatting Operations in v3.3 makes no mention of deprecation plans.
This is not quite certain enough for me to consider it actionable, however, and it would be nice to find a source with a clear statement.

Related

why ast.literal_eval('1+1') is wrong in python2.7 [duplicate]

This question already has answers here:
Why does ast.literal_eval('5 * 7') fail?
(2 answers)
Closed 5 years ago.
When I execute code ast.literal_eval('1+1') in python2.7, the result as follows:
And I try it in python3.6, it works correctly. So what's the reason?
Both the 2.7 and 3.6 docs say the following:
This can be used for safely evaluating strings containing Python values from untrusted sources without the need to parse the values oneself. It is not capable of evaluating arbitrarily complex expressions, for example involving operators or indexing.
Addition is an operator, so this is documented not to work. The fact that it does work in Python 3.6 is surprising to me. Searching the bug tracker, this discrepancy is listed as Python bug #31778.

What does the underscore mean in python [duplicate]

This question already has answers here:
Why does python use two underscores for certain things? [duplicate]
(7 answers)
Closed 9 years ago.
Hi everyone as it obvious from my question I am like a brand new to python.
I am so confused when I am reading the documentation on python or even here in the Stackoverflow forum...
Why do they write like that
from __future__ import division
What does the underscore around the Future word mean ?? And Are we supposed to use it like that with the underscore in the python interpreter ?
This is just one of tons of examples. Any help would be greatly appericated.
According to PEP 236 where this module was proposed, the double underscores make it a reserved name.
[5] This ensures that a future_statement run under a release prior to
the first one in which a given feature is known (but >= 2.1) will
raise a compile-time error rather than silently do a wrong thing.
If transported to a release prior to 2.1, a runtime error will be
raised because of the failure to import __future__ (no such module
existed in the standard distribution before the 2.1 release, and
the double underscores make it a reserved name).

When is semicolon use in Python considered "good" or "acceptable"?

Python is a "whitespace delimited" language. However, the use of semicolons are allowed. For example, the following works, but it is frowned upon:
print("Hello!");
print("This is valid");
I've been using Python for several years now, and the only time I have ever used semicolons is in generating one-time command-line scripts with Python:
python -c "import inspect, mymodule; print(inspect.getfile(mymodule))"
Or adding code in comments on Stack Overflow (i.e., "you should try import os; print os.path.join(a,b)")
I also noticed in this answer to a similar question that the semicolon can also be used to make one line if blocks, as in
if x < y < z: print(x); print(y); print(z)
which is convenient for the two usage examples I gave (command-line scripts and comments).
The above examples are for communicating code in paragraph form or making short snippets, but not something I would expect in a production codebase.
Here is my question: in Python, is there ever a reason to use the semicolon in a production code? I imagine that they were added to the language solely for the reasons I have cited, but it’s always possible that Guido had a grander scheme in mind.
No opinions please; I'm looking either for examples from existing code where the semicolon was useful, or some kind of statement from the python docs or from Guido about the use of the semicolon.
PEP 8 is the official style guide and says:
Compound statements (multiple statements on the same line) are generally discouraged.
(See also the examples just after this in the PEP.)
While I don't agree with everything PEP 8 says, if you're looking for an authoritative source, that's it. You should use multi-statement lines only as a last resort. (python -c is a good example of such a last resort, because you have no way to use actual linebreaks in that case.)
I use semicolons in code all of the time. Our code folds across lines quite frequently, and a semicolon is a definitive assertion that a statement is ending.
output, errors, status = generate_output_with_errors_and_status(
first_monstrous_functional_argument(argument_one_to_argument
, argument_two_to_argument)
, second_argument);
See? They're quite helpful to readability.

Python: How to convert tabs to spaces? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Enforce “spaces” or “tabs” only in python files?
I got Python code that has mixed tabs and spaces and is very difficult to read or understand the indentation, because lines look like they are at a certain indentation in the IDE but Python parses them as a different indentation than what they look like. Do tabs in Python count for a certain hard-coded number of spaces? Is there a way to canonicalize a Python script that has mixed tabs/spaces, to use consistent spacing?
There is no canonical value for the number of spaces that = 1 tab in python (I like 4, but that's just me).
What you can do is read the file in and search for \t characters, and replace those with however many spaces you need.
EDIT: Something that will probably be useful to you in the future is Python's style guide (aka PEP8)

Python 3 Syntax Changes

So my work which had used older Python 2 is doing some code updating, anyways I am just learning python and am actually pretty new here, but what are the major syntax changes that went from 2-->3
Or is there really even that much syntax changes at all (like I know print got changed, but what else MAJOR)
Thanks
What’s New In Python 3.0:
http://docs.python.org/release/3.0.1/whatsnew/3.0.html
PEP: 3000 - Python 3000:
http://www.python.org/dev/peps/pep-3000/
PEP: 3099 - Things that will Not Change in Python 3000:
http://www.python.org/dev/peps/pep-3099/
Did you read this: Overview of Syntax Changes ?
The things you really notice in the syntax are the print statement, and the change in the exception syntax. 2to3 will handle all that.
That won't cause you any headaches though. Those generally come from the split of strings into binary bytes and Unicode strings. 2to3 doesn't handle that.
So the changes in syntax aren't really what you need to worry about. :)
Then there are some minor changes in the syntax, tons of small changes in various functionality and a huge reorganization of the standard library, most of which 2to3 handles.
There isn't any canonical summary of all changes afaik, although I've tried to make one in my new book. There may be some misses, but there you go.
You can't do much better than to read the documentation: http://docs.python.org/release/3.1.2/whatsnew/ covers all the changes pretty succienctly. Read the "What’s New In Python 3.0" section first for the main changes.

Categories