Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

i came across this completely randomly just curious because this this doesn't even look like a list .

like image 511
FIcti_0n Avatar asked Oct 17 '22 08:10

FIcti_0n


2 Answers

This is a weird side-effect of the way the auto-quoting feature is implemented in IPython. In particular, every line entered at the IPython terminal is pattern-matched against this regex 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 the input ', = what iss this', this results in 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'

Since esc is a comma, the AutoHandler prefilter reaches this if-else statement:

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

which modifies the command to become

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")'

So in summary,

  • the initial comma triggers IPython's auto-quoting feature.
  • since no valid function name was found, the ifun is an empty string
  • auto-quoting quotes the rest of the command string and forms

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

    as the new command. This "command" is then evaluated.

Hence, the result returned is the tuple ("=", "what", "iss", "this").

like image 89
unutbu Avatar answered Oct 21 '22 08:10

unutbu


Just using ? in IPython to see Introduction and overview of IPython's features.

  1. Auto-Quoting

    You can force auto-quoting of a function's arguments by using ',' as the first character of a line. For example::

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

    If you use ';' instead, the whole argument is quoted as a single string (while ',' splits on whitespace)::

      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")
    

    Note that the ',' MUST be the first character on the line! This won't work::

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

EDIT: Sorry for the = explanation. As @randomir said, the = op discusses is here.

like image 36
danche Avatar answered Oct 21 '22 07:10

danche