2012-09-24 2 views
2

으로 지정된 함수를 실행할 수 없습니다. IPython의 마법 %paste% 함수를 사용하여 지정된 함수를 실행할 수 있습니까?% paste %

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]: 

답변

4

은 페이스트 마법 %paste는 (더 % 후행 없음)된다

귀하의 경우에는 어떻게됩니까
In [3]: %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 -- 

In [4]: add_to_index 
Out[4]: <function __main__.add_to_index> 

당신이 %paste에 대한 선택적 인수를 사용하고 있습니다 :

In [5]: %paste? 
Type:  Magic function 

...(text omitted) 

You can also pass a variable name as an argument, e.g. '%paste foo'. 
This assigns the pasted block to variable 'foo' as string, without 
dedenting or executing it (preceding >>> and + is still stripped) 

을 붙여 넣은 코드가 실행되지 않는다면, 인수로 지정한 변수에 할당됩니다 (% 귀하의 경우).