简体   繁体   中英

Any limit to how many characters Python's IDE can take in one line?

I'm a bit curious as I want to create a file containing a dictionary that would automatically update whenever an event occurs and need to know if there is a limit to the number of characters Python's IDE can hold on a single line.

An alternative for me would be at least knowing a way to make the dictionary always start from a new line when it reached a certain length. But I'd still like to know if there is a limit just for knowing's sake.

As far as disk storage and ram memory are concerned, '\\n is just another character. As far as tcl/tk and Python's tkinter wrapper are concerned, newlines are very important. Since tk's Text widget is intended to display text to humans, and since vertical scrolling is much more useful for this than horizontal scrolling, it is optimized for the former. A hundred 1000 char lines (100,000 chars total) bogs down vertical scrolling, whereas 300,000 50 char lines (15,000,000 chars total) is no problem. IDLE uses tkinter and the main windows are based on tk's Text widget. So if you to view text in IDLE, keep lines lengths sensible.

I do not know about other IDEs that use other GUI frameworks. But even if they handle indefinitely long lines better, horizontal scrolling of a 100000 char line is pretty obnoxious.

You shouldn't have to worry about a limit. As for a new line, you can use an if with \\n or a for loop depending on what you're going for.

To answer your question, no, there's no limit on the length of the dictionary or list (or any other object). I've stored all the words of an entire book in a list. They are just streams of input.

As far as how your code is written, it's not Pythonic to write beyond 80 characters/columns per line, because, ultimately, humans must read it.

But if it makes you feel any better, you can break dictionaries and lists up onto multiple lines, and even leave comments between the items:

>>> my_dict = {'a': 1,  # comment 1
               'b': 2,  # comment 2
               'c': 3,  # comment 3
               'd': 4
              }

You can also break any statement with a backslash:

>>> my_string = "my \
                   string"

>>> print(my_string)
'my string'

Lastly, you can use triple quotes for very long strings, that may span multiple lines. Note, newlines will be included in your string.

>>> my_very_long_string = '''This is a long string.
                   Rather than using multiple print statements,
                   I only used one.'''

>>> print(my_very_long_string)
'This is a long string.\nRather than using multiple print statements,\nI only used one.'

In the last (multi-line string) example, this is how a "file" (stream) actually looks when it is not being formatted to be presented on multiple lines in an editor.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM