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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T02:24:22+00:00 2026-06-09T02:24:22+00:00

This may be a simple question, but I’m having trouble making a unique search

  • 0

This may be a simple question, but I’m having trouble making a unique search for it.

I have a class that defines a static dictionary, then attempts to define a subset of that dictionary, also statically.

So, as a toy example:

class example(object): 
    first_d = {1:1,2:2,3:3,4:4} 
    second_d = dict((k,first_d[k]) for k in (2,3))

This produces NameError: global name 'first_d' is not defined

How should I be making this reference? It seems this pattern works in other cases, eg:

class example2(object):
    first = 1
    second = first + 1
  • 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-09T02:24:23+00:00Added an answer on June 9, 2026 at 2:24 am

    A basic list comprehension has the following syntax

    [expression for var in iterable]
    

    When a list comprehension occurs inside a class, the attributes of the class
    can be used in iterable. This is true in Python2 and Python3.

    However, the attributes of the class can be used (i.e. accessed) in expression in Python2 but not in Python3.

    The story is a bit different for generator expressions:

    (expression for var in iterable)
    

    While the class attributes can still be accessed from iterable, the class attributes are not accessible from expression. (This is true for Python2 and Python3).

    This can all be summarized as follows:

                                 Python2      Python3
    Can access class attributes
    --------------------------------------------------
    list comp. iterable                Y            Y
    list comp. expression              Y            N
    gen expr. iterable                 Y            Y
    gen expr. expression               N            N
    dict comp. iterable                Y            Y
    dict comp. expression              N            N
    

    (Dict comprehensions behave the same as generator expressions in this respect.)


    Now how does this relate to your question:

    In your example,

    second_d = dict((k,first_d[k]) for k in (2,3))
    

    a NameError occurs because first_d is not accessible from the expression part of a generator expression.

    A workaround for Python2 would be to change the generator expression to a list comprehension:

    second_d = dict([(k,first_d[k]) for k in (2,3)])
    

    However, I don’t find this a very comfortable solution since this code will fail in Python3.

    You could do as Joel Cornett suggests:

    second_d = {k: v for k, v in first_d.items() if k in (2, 3)}
    

    since this uses first_d in the iterable rather than the expression part of the dict comprehension. But this may loop through many more items than necessary if first_d contains many items. Neverthess, this solution might be just fine if first_d is small.

    In general, you can avoid this problem by defining a helper function which can be defined inside or outside the class:

    def partial_dict(dct, keys):
        return {k:dct[k] for k in keys}
    
    class Example(object):
        first_d = {1:1,2:2,3:3,4:4}
        second_d = partial_dict(first_d, (2,3))
    
    class Example2(object):
        a = [1,2,3,4,5]
        b = [2,4]
        def myfunc(A, B):
            return [x for x in A if x not in B]
        c = myfunc(a, b)
    
    print(Example().second_d)
    # {2: 2, 3: 3}
    
    print(Example2().c)
    # [1, 3, 5]
    

    Functions work because they define a local scope and
    variables in this local scope can be accessed from within the dict comprehension.

    This was explained here, but I am not entirely comfortable with this since it does not explain why the expression part behaves differently than the iterable part of a list comprehension, generator expression or dict comprehension.

    Thus I can not explain (completely) why Python behaves this way, only that this is the way it appears to behave.

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

Sidebar

Related Questions

Okay this may be a simple question but I have yet to come with
may be too simple groovy question but....please help i have a list like this:
This may be a simple question, but I have been Googling for over an
This may seem like a very simple question, but I have been struggling with
this may be a simple question but i was wondering, i have an application
This may be a simple C# question but I need a solution. I have
This seems like a simple question, but I am having trouble finding the answer.
This may be very simple question but I have been looking around for a
So this may be a very simple question that I'm overthinking but if I
This may be a simple question but I can;t find the answer anywhere. Here

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.