Why does Python automatically combine two seperate strings? -


if forget comma in list of strings, python (2.7) automatically concatenate 2 strings. example:

x = ['id', 'date' 'time'] print(x) #[id, 'datetime'] 

this behavior seems run contrary vast majority of "use cases" user forgot comma. leads either missing error in code or getting odd error message. in example above, instance, list of attributes object , threw attributeerror: 'yourobjectclass' object has no attribute 'datetime'. feature or bug, , if it's feature why feature?

it's feature, , rationale given in the part of spec describes it:

multiple adjacent string literals (delimited whitespace), possibly using different quoting conventions, allowed, , meaning same concatenation. thus, "hello" 'world' equivalent "helloworld". feature can used reduce number of backslashes needed, split long strings conveniently across long lines, or add comments parts of strings, example:

re.compile("[a-za-z_]"       # letter or underscore            "[a-za-z0-9_]*"   # letter, digit or underscore           ) 

Comments

Popular posts from this blog

PHP DOM loadHTML() method unusual warning -

python - How to create jsonb index using GIN on SQLAlchemy? -

c# - TransactionScope not rolling back although no complete() is called -