What does the operator do when you use it with the = sign?

I stumbled upon it quite by accident, just curious, because it doesn’t even look like a list.

+4
source share
2 answers

This is a strange side effect of how the auto-quote function is implemented in IPython. In particular, each line entered on the IPython terminal maps to this regular expression pattern :

import re
line_split = re.compile("""
             ^(\s*)               # any leading space
             ([,;/%]|!!?|\?\??)?  # escape character or characters
             \s*(%{0,2}[\w\.\*]*)     # function/method, possibly with leading %
                                  # to correctly treat things like '?%magic'
             (.*?$|$)             # rest of line
             """, re.VERBOSE)

In the case of input, ', = what iss this'this leads to the following assignments :

pre, esc, ifun, the_rest = line_split.match(', = what iss this').groups()
print(repr(pre))
# ''

print(repr(esc))
# ','

print(repr(ifun))
# ''

print(repr(the_rest))
# '= what iss this'

esc , AutoHandler if-else:

    if esc == ESC_QUOTE:
        # Auto-quote splitting on whitespace
        newcmd = '%s("%s")' % (ifun,'", "'.join(the_rest.split()) )

,

In [19]: ifun=''

In [20]: the_rest='= what iss this'

In [21]: newcmd = '%s("%s")' % (ifun,'", "'.join(the_rest.split()) )

In [22]: newcmd
Out[22]: '("=", "what", "iss", "this")'

, ,

  • IPython.
  • , ifun -
  • -

    '%s("%s")' % (ifun,'", "'.join(the_rest.split()) )
    

    . "" .

, ("=", "what", "iss", "this").

+2

? IPython, IPython.

  1. -

    , ','   . :

      In [1]: ,my_function /home/me   # becomes my_function("/home/me")
    

    ';'  string (while ',' )::

      In [2]: ,my_function a b c   # becomes my_function("a","b","c")
      In [3]: ;my_function a b c   # becomes my_function("a b c")
    

    , ',' !   :

      In [4]: x = ,my_function /home/me    # syntax error
    

EDIT: =. @randomir, = op .

+1

Source: https://habr.com/ru/post/1683104/


All Articles