Is it possible to run a function that is specified by using the magic %paste% function in IPython?
In [1]: %paste%
def add_to_index(index,keyword,url):
for e in index:
if e[0] == keyword:
if url not in e[1]:
e[1].append(url)
return
index.append([keyword,[url]])
## -- End pasted text --
Block assigned to '%'
In [2]: %whos
Variable Type Data/Info
-----------------------------
% SList ['def add_to_index(index,<...>append([keyword,[url]])']
In [3]: add_to_index
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-3-e3075a18cb0c> in <module>()
----> 1 add_to_index
NameError: name 'add_to_index' is not defined
In [4]: add_to_index(index, 'test', 'http://test.com')
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-4-580237464b17> in <module>()
----> 1 add_to_index(index, 'test', 'http://test.com')
NameError: name 'add_to_index' is not defined
In [5]:
The paste magic is
%paste(no trailing%):What happens in your case is that you are using the optional argument for
%paste:When you do that the pasted code does not get executed, it is just assigned to the variable you gave as an argument (
%in your case).