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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T18:09:11+00:00 2026-05-28T18:09:11+00:00

Example: class A: a = 1 class B(A): b = 2 y = b

  • 0

Example:

class A:
    a = 1

class B(A):
    b = 2
    y = b # works fine
    x = a # NameError: name 'a' is not defined
    x = A.a # works fine

z = B()
z.a # works fine
B.a # works fine

Why is x = a not allowed? In every other context (access through an instance, access through the subclass name) it works fine; but somehow inside the class itself, it doesn’t work.

And given this behavior, it seems I cannot implement a class hierarchy where each class defines some additional attributes – since I won’t be able to access them in the subclass without knowing exactly where in the hierarchy they are defined.

Here’s what I was trying (unsuccessfully) to do:

class X:
  accelerate = compose(f1, f2, f3) # f1, f2, f3 are functions

class Y(X):
  move = compose(f4, f5)
  stop = f6

class Z(Y):
  action = compose(accelerate, stop)

class U(Y):
  action = compose(move, stop)

These classes wouldn’t have been initialized at all; I just wanted to use them to create a hierarchy of functions.

  • 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-05-28T18:09:13+00:00Added an answer on May 28, 2026 at 6:09 pm

    When you write this:

    class B(A):
        b = 2
        y = b # works fine
        x = a # NameError: name 'a' is not defined
        x = A.a # works fine
    

    What Python does is create a new scope (stored in a dictionary), execute all your definitions, then at the end of the class block it passes the dictionary to the type class (unless you’ve set another metaclass) to create your new class. It’s roughly equivalent to this:

    B = type('B', (A,), classdict_from_scope)
    

    Before you get to that stage, the class B doesn’t exist, let alone have any base classes to inherit attributes from. Consider that you could have multiple base classes, and the resolution order of names looked up in those classes is complex and depends on the full set of them and all their bases; this order isn’t determined until your child class B is actually created.

    This means that when Python comes to execute x = a, it finds a not in scope, and it’s not doing an attribute lookup on a class or instance, so it can’t follow the name resolution protocol to look for an alternative binding. So all it can do is throw an error.

    So that’s why Python works that way. What can you do about it?

    You have two basic options.

    1. You can specify the class you want to look for the attributes in explicitly.
    2. You can lookup the attributes in your subclass after it is created.

    Note that (1) is not that bad if you’re only using single inheritance; you can just look up all of the attributes in A; if they’re only defined in a parent class of A this will still find them, so you don’t actually need to know where in the hierarchy they are defined.

    If you have multiple inheritance, then you would have to know at least which hierarchy contained the attribute you wanted to look up. If that’s difficult or impossible, then you can use option (2), which would look something like this:

    class B(A):
        b = 2
        y = b
    
    B.x = B.a
    

    This looks a little ugly, but constructs an identical class B as you would have if you created x inside the class block, and the transient class B without x can never be seen by any other code if you put the B.x assignment directly after the class block. (It might not have identical results if you’re using class decorators, though)

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

Sidebar

Related Questions

Example for threading queue book Accelerated C# 2008 (CrudeThreadPool class) not work correctly. If
I have models (simplified example): class Group(models.Model): name = models.CharField(max_length = 32) class Person(models.Model):
I have a class that defines a function with default parameters. It works fine
In the following example, GetFilteredCustomers() works fine so I can send various letters which
I've tried this example and it works fine with the example URL ( http://search.yahoo.com/search
I have to make dynamic hashes, so the class example won't work since the
Example class in pseudocode: class SumCalculator method calculate(int1, int2) returns int What is a
Example: class MyClass { Composition m_Composition; void MyClass() { m_Composition = new Composition( this
Please consider this example class: [Serializable] public class SomeClass { private DateTime _SomeDateTime; public
I have the following example class: Test.h: @interface Test : UIButton { NSString *value;

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.