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

  • Home
  • SEARCH
  • 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 8697575
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T01:33:35+00:00 2026-06-13T01:33:35+00:00

According to Python documentation, both dir() (without args) and locals() evaluates to the list

  • 0

According to Python documentation, both dir() (without args) and locals() evaluates to the list of variables in something called local scope. First one returns list of names, second returns a dictionary of name-value pairs. Is it the only difference? Is this always valid?

assert dir() == sorted( locals().keys() )
  • 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-13T01:33:37+00:00Added an answer on June 13, 2026 at 1:33 am

    The output of dir() when called without arguments is almost same as locals(), but dir() returns a list of strings and locals() returns a dictionary and you can update that dictionary to add new variables.

    dir(...)
        dir([object]) -> list of strings
    
        If called without an argument, return the names in the current scope.
    
    
    locals(...)
        locals() -> dictionary
    
        Update and return a dictionary containing the current scope's local variables.
    

    Type:

    >>> type(locals())
    <type 'dict'>
    >>> type(dir())
    <type 'list'>
    

    Update or add new variables using locals():

    In [2]: locals()['a']=2
    
    In [3]: a
    Out[3]: 2
    

    using dir(), however, this doesn’t work:

    In [7]: dir()[-2]
    Out[7]: 'a'
    
    In [8]: dir()[-2]=10
    
    In [9]: dir()[-2]
    Out[9]: 'a'
    
    In [10]: a
    Out[10]: 2
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What exactly do *args and **kwargs mean? According to the Python documentation, from what
According to Jython's documentation : Jython is an implementation of the Python language for
According to the Python documentation on idioms and anti-idioms in relation to exceptions :
According to python documentation assert statements aren't included in code if it is compiled
According to the Python documentation, only a few hash algorithms are guaranteed to be
According to the official Python documentation for the weakref module the primary use for
According to the documentation on python's getopt (I think) the options fields should behave
According to my interpretation of Python 2.7.2 documentation for Built-In Types 5.7 Set Types
According to the documentation , if Django was installed manually like python setup.py install
I am using the MongoEngine map_reduce functionality in python. According to the MongoDB documentation

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.