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 8968767
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T17:30:00+00:00 2026-06-15T17:30:00+00:00

I would like to write a function that pickles all objects in the current

  • 0

I would like to write a function that pickles all objects in the current namespace which are instances of classes from a given module. the idea is that during an ipython session a user creates many objects from mymodule, and may need to save all of them quickly.

so, for example i want something like this

from mymodule import cat, dog, pickle_all
c= cat(), d=dog()
pickle_all('killer_sesh.p')

end session, forget about for a month, come back and start new session,

from mymodule import upickle_all 
objs = unpickle_all('killer_sesh.p')

so my first attempt (show below) works when pasted into the current namespace. if i define as a function, or put it somewhere else, like in mymodule, then the dir() command doesnt return the current namespace, but the namespace as seen by the function. even if i pass the result of dir() as an argument, the objects are not available within the function.

import pickle
filename= 'killer_sesh.p'
module='mymodule'
module_objects = {}
for k in dir():
    try:
        if eval(k).__module__.split('.')[0] == module:
            if k[0]!='_':
                print(k)
                module_objects[k] = eval(k)
    except(AttributeError):
        pass
    file= open(filename,'w')
pickle.dump(module_objects, file)
file.close()

is a function like this possible?

  • 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-15T17:30:01+00:00Added an answer on June 15, 2026 at 5:30 pm

    if i define as a function, or put it somewhere else, like in mymodule, then the dir() command doesnt return the current namespace, but the namespace as seen by the function. even if i pass the result of dir() as an argument, the objects are not available within the function.

    Right, that’s because you’re using eval to evaluate them, which evaluates them in the current namespace. You could pass the globals and locals from a different mainspace, and call eval with those parameters. In other words, instead of foo(dir()), foo(dir(), globals(), locals())`.

    However, this whole design is a bad idea. Using eval to get the members of a scope is a bad idea. In fact, using eval for almost anything is a bad idea.

    A much better solution is to pass the thing you want to evaluate the members of, and use getattr to get them.

    And an even better solution is to avoid using dir and then figuring out how to get the attributes with those names; if you want to inspect things, that’s what the inspect module is for. (And even when you want to do something that inspect doesn’t quite offer, reading its source code—which is linked from the docs—will usually tell you the best way to do it.)

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

Sidebar

Related Questions

I would like to write a function which will read values from a text
I would like to write a function in Python that takes a string which
I would like to write a function that has a loop in it which
I would like to write a function that has a loop in it which
I would like to write a function that creates a plot of a set
The following is for Python 3.2.3. I would like to write a function that
In clojure, I would like to write a tail-recursive function that memoizes its intermediate
I'd like to write a function that would have some optional code to be
In Python, I'd like to write a function that would pretty-print its results to
I would like to write a cross-platform function in C++ that contains system calls.

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.