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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T01:20:51+00:00 2026-06-09T01:20:51+00:00

Background : Migrating from R to Python with numpy/scipy. Trying to make a little

  • 0

Background:
Migrating from R to Python with numpy/scipy. Trying to make a little module of useful functions. In particular, I’m trying to create a recursive element-type checker.

Question:
Is it possible to get a list of the current valid types in the Python environment a function is being called in?

For example, isinstance(1,int) will return True, isinstance(1,str) will return False, but isinstance(1,asdf) will throw a NameError: name 'asdf' is not defined i.e. int and str are defined, but asdf is not. How can I get the list of types that are defined, or names present in the current Python environment, and filter them by types?

  • 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-09T01:20:53+00:00Added an answer on June 9, 2026 at 1:20 am

    In Python, types are themselves ordinary objects. That is, for example,

    type('hello') == str
    type(5) == int
    type(int) == type
    type(type) == type
    

    are all True.

    So to do this, look for all variables in scope that point to objects of type type.

    To get all objects in scope, look at both dir() (which excludes built-in names like int) and dir(__builtins__) (the built-in names) locals() (variables defined in the current function), globals() (variables defined outside of functions in the current module), and vars(__builtins__) (the built-in names). These are all dictionaries from name => object, so combine them all and get the objects:

    objs = dict(vars(__builtins__), **dict(globals(), **locals())).values()
    

    and filter only types:

    types_in_scope = [o for o in objs if isinstance(o, type)]
    

    Note that these are just the variables in scope that point to types. It’s quite possible to have a reference to an object whose type is not assigned to any variable in scope. For example:

    def foo():
        class Foo:
            pass
        return Foo()
    x = foo()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Background: Migrating an application from ball of mud to MVC. Many classes contain HTML
We are coming from a Visual Source Safe background and are trying to look
Background: I'm working on migrating from SQL Server 2000 to SQL Server 2005. This
I come from a Subversion background, but I am slowly migrating to Mercurial. When
Background: I'm using the (fantastic) Vim plugin python-mode , which includes the pep8 linter.
Background : I'm trying to convert some JavaScript code which uses the the Crossfilter
Background: Using unix, codeigniter from localhost. I'd like to run a controllers via a
I am coming from a novice database programmer background, mainly using MS Access for
[background below] I've got my data modelled out in SQLObject in Python on the
I have an app that I am migrating from Ruby to JRuby (due to

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.