Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do I get the empty tuple if I type / in iPython?

Tags:

ipython

Open up iPython and type this:

/

Hit enter and wonder about the result:

()

You cannot assign it, my guess it has something to do with the shell functionality.

Edit:

You can assign it with:

 p = Out[xx]

But not directly:

 p = / 

will give:

SyntaxError

It is indeed an empty tuple.

like image 944
Edgar Klerks Avatar asked Feb 16 '15 12:02

Edgar Klerks


People also ask

How do you fill an empty tuple in Python?

There are two ways to initialize an empty tuple. You can initialize an empty tuple by having () with no values in them. You can also initialize an empty tuple by using the tuple function. A tuple with values can be initialized by making a sequence of values separated by commas.

What is an empty tuple?

You can create empty tuple object by giving no elements in parentheses in assignment statement. Empty tuple object is also created by tuple() built-in function without any arguments >>> T1 = () >>> T1 () >>> T1 = tuple() >>> T1 () Pythonic. © Copyright 2022.

What is the point of an empty tuple in Python?

Tuples represent arbitrary sequences of values. When you need to describe an empty sequence of values, you can use an empty tuple.

Is an empty tuple valid?

In python, an empty tuple always evaluates to false.


1 Answers

It is a convenience feature for callable objects/names. It's not an empty tuple, but parentheses completion. From iPython's help system (?):

  • Auto-parentheses and auto-quotes (adapted from Nathan Gray's LazyPython)

    1. Auto-parentheses

      Callable objects (i.e. functions, methods, etc) can be invoked like this (notice the commas between the arguments)::

      In [1]: callable_ob arg1, arg2, arg3

      and the input will be translated to this::

      callable_ob(arg1, arg2, arg3)

      This feature is off by default (in rare cases it can produce undesirable side-effects), but you can activate it at the command-line by starting IPython with --autocall 1, set it permanently in your configuration file, or turn on at runtime with %autocall 1.

      You can force auto-parentheses by using '/' as the first character of a line. For example::

      In [1]: /globals # becomes 'globals()'

like image 176
Michael Foukarakis Avatar answered Nov 15 '22 08:11

Michael Foukarakis