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)
Related
This question already has answers here:
What is the max length of a Python string?
(3 answers)
Closed 4 years ago.
do you have any experience with such a long dictionaries in one line 20.000? I am working on a module that has such. First I imported this dictionary with json, but I feel like it's better to import as least as possible. The code is not meant to be ever opened by the users and is working well for now. Do you have any experience with line that has 20.000 characters? can it cause any problems in the future?
Thanks
One practical problem you could run into is LINE_MAX. On *nix systems, that's the maximum length of a line which is guaranteed to be supported by all the usual utilities like grep and so on. The value promised by POSIX is 2048 bytes, but some systems have a larger value like 4096.
So, if you do decide to have 20,000 characters on a single line, you can't expect the usual utility programs to be able to operate on that file.
Ref: https://www.gnu.org/software/libc/manual/html_node/Utility-Minimums.html
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.
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).
This question already has answers here:
Rewrite multiple lines in the console
(7 answers)
Closed 9 years ago.
I am currently working on a text-adventure, which should have a more or less complex fight system. To create such, I decided to generate a 16x16 tile battlefield, which will be represented by ASCII-characters. But, because a fight may take more than turn, I don't want to reprint the battlefield multiple times, but delete the "old" one and print the new situation to the same place. But I suppose that it won't work with sys.stdout.write() and sys.stdout.flush() since there have to be removed multiple lines. So my question is: how do I accomplish my goal? At the moment I open a new console window, where everything is reprinted, which is ahem... not very elegant.
Another problem would be the cross-platform use of the programm.
Check out the curses module (http://docs.python.org/2/library/curses.html).
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.