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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T21:02:13+00:00 2026-06-04T21:02:13+00:00

Below is a partial class definition of mine: class Trial: font = pygame.font.Font(None, font_size)

  • 0

Below is a partial class definition of mine:

class Trial:
    font = pygame.font.Font(None, font_size)
    target_dic = {let: font.render(let, True, WHITE, BG) for let in list("ABCDEFGHJKLMNPRSTUVWX")}

The last line of the partial class definition, target_dic = {let: font.render(let, True, WHITE, BG) for let in list("ABCDEFGHJKLMNPRSTUVWX") returns the error: global name ‘font’ is not defined. Fair enough.

However, I tried the following test case and got no error:

class x:
    dat = 1
    datlist = [dat for i in range(10)]

Why should the first case not work? Doesn’t the member font exist by the time the dictionary comprehension is reached?

Do I need to move these operations to __init__ or is it possible to define the list exactly once when the class object is created?

EDIT:

For clarity, I’d like to be able to populate the list at class object creation time to cut down on the time spent creating Trial objects.

  • 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-04T21:02:15+00:00Added an answer on June 4, 2026 at 9:02 pm

    Partial answer, as it is more to cut some wrong paths to follow.

    If I take back your working sample and put a dict comprehension:

    class x:
        dat = 1
        datlist = {i:dat for i in range(10)}
    

    I get also this:

    >>> NameError: global name 'dat' is not defined
    

    So it looks like dict comprehension is hiding the temporary dict use during class statement execution, but the list comprehension doesn’t.

    No further information found by now in the docs about this…

    Edit based on @interjay comment:
    class construction not fulfilling scope norm is addressed in this post. Short story is that list comprehension are buggy in 2.x and see class members but they shouldn’t.

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

Sidebar

Related Questions

Please see the code below: public partial class Form1 : Form { public Form1()
I have silverlight usercontrol. This contains Service Entity object. see below public partial class
I have the class defined as below public partial class School { public StudentDetails
During the Tailspin Spyworks tutorial (link below) the author adds a partial class for
The below is not valid- public partial class form1 : Form : ICloneable Is
I've a class similar to one below: namespace BLL { public partial class SomeClass
I have the below code-behind: public partial class MainWindow : Window { public MainWindow()
Can someone tell me why the below code is not working? public partial class
My function below calls a partial view after a user enters a filter-by string
Below is the code from internalRegister method of GCMRegistrar class static void internalRegister(Context context,

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.