Why python allows spaces between the object and the method name after ".".

Does anyone know why python allows you to set an unlimited number of spaces between an object and the name of a method called ".".

Here are some examples:

 >>> x = []
 >>> x.            insert(0, 'hi')
 >>> print x
 ['hi']

Another example:

>>> d = {}
>>> d            ['hi'] = 'there'
>>> print d
{'hi': 'there'}

The same goes for classes.

>>> myClass = type('hi', (), {'there': 'hello'})
>>> myClass.            there
'hello'

I am using python 2.7 I tried to execute some google searches and look at the python source code, but I can not find the reasons why this is allowed.

+4
source share
3 answers

.acts as an operator. You can do the obj . attrsame as you can do this + thator this * thator the like. Link says:

, .

, , . .. , .

+12

, / , , - .

, . , . . :

escaped_html = text.replace('&', '&amp;').replace('<', '&lt;').replace('>'. '&gt;')

, . , :

fooinstance \
    .bar('a really long argument is passed in here') \
    .baz('and another long argument is passed in here')

, escapes \, . , . , ( / ), , , .

, Python :

fooinstance = fooinstance.bar('a really long argument is passed in here')
fooinstance = fooinstance.baz('and another long argument is passed in here')

.

+1

. - ( "" ), . (C , ), , . Python, , ; , , . [, , , .]

, - :

obj.deeply.\
    nested.\
    chain.of.\
    attributes

, , , , , , nested , . deeply.

In expressions with a deeper nesting, a small amount of extra spaces can give a large readability coefficient:

For comparison:

x = your_map[my_func(some_big_expr[17])]

vs

x = your_map[ my_func( some_big_expr[17]) ]

Caveats: If your employer, client, team, or professor has style rules or recommendations, you must adhere to them. The second example above does not match the Python style guide, PEP8, which most Python stores accept or adapt. But this document is a compilation of guidelines, not religious or civil decrees.

+1
source

All Articles