Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8596829
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T00:46:24+00:00 2026-06-12T00:46:24+00:00

Is it possible to run a function that is specified by using the magic

  • 0

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]:
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T00:46:25+00:00Added an answer on June 12, 2026 at 12:46 am

    The paste magic is %paste (no trailing %):

    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>
    

    What happens in your case is that you are using the optional argument for %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)
    

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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Run a simple shell command Is there a Win32 API function that
Is it possible to run Tornado such that it listens to a local port
Is it possible to run hip-hop php in cygwin which is using more script
We want to know if it is possible to have a function using jQuery
I have this function I am using to decrypt values that works fine on
Is it possible to run executable files in google app engine? Like by using
Do you know if it's possible to run jQuery on a variable that contains
Is it possible for a visitor to a page to run an arbitrary function
It is possible to run bcp utility for sybase database from .net code? I
Is it possible to run nose test generators inside custom classes? I am trying

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.