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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T23:22:59+00:00 2026-06-10T23:22:59+00:00

In a recent project I try to do something like this (more complex, but

  • 0

In a recent project I try to do something like this (more complex, but the same result):

class MetaA(type):
    def __new__(cls, name, args, kwargs):
        print kwargs["foo"]
        return type.__new__(cls, name, args, kwargs)

class A(object):
    __metaclass__ = MetaA
    foo = "bar"

class B(A):
    pass

I get this:

bar
Traceback (most recent call last):
  File "C:\Users\Thorstein\Desktop\test.py", line 10, in <module>
    class B(A):
  File "C:\Users\Thorstein\Desktop\test.py", line 3, in __new__
    print kwargs["foo"]
KeyError: 'foo'

Are class attributes not inherited? If so, is there any workaround possible in a similar framework to the above?

EDIT:
Might be easier to see what I mean using an actual (simplified) example from the program..

class GameObjectMeta(type):
    def __new__(cls, name, bases, attrs):
        attrs["dark_color"] = darken(*attrs["color"])
        return type.__new__(cls, name, bases, attrs)


class GameObject(object):
    __metaclass__ = GameObjectMeta
    color = (255,255,255)   # RGB tuple

class Monster(GameObject):
    pass

Basically, want to run a function on the base color to make a darker one that’s saved in the class (multiple instances of the class will want the same color, but there will be a longer class hierarchy). I hope this’ll make more sense..

  • 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-10T23:23:00+00:00Added an answer on June 10, 2026 at 11:23 pm

    It’s not supposed to inherit those. The metaclass receives the attributes defined on the class it is instantiating, not those of its base classes. The whole point of the metaclass constructor is to get access to what is actually given in the class body.

    In fact, the attributes of the base class really aren’t “in” the subclass at all. It’s not as if they are “copied” to the subclass when you define it, and in fact at creation time the subclass doesn’t even “know” what attributes its superclasses might have. Rather, when you access B.foo, Python first tries to find the attribute on B, then if it doesn’t find it, looks on its superclasses. This happens dynamically every time you try to read such an attribute. The metaclass mechanism isn’t supposed to have access to attributes of the superclass, because those really aren’t there on the subclass.

    A perhaps related problem is that your definition of __new__ is incorrect. The arguments to the metaclass __new__ are cls, name, bases, and attrs. The third argument is the list of base classes, not “args” (whatever you intend that to be). The fourth argument is the list of attributes define in the class body. If you want access to inherited attributes you can get at them via the bases.

    Anyway, what are you trying to accomplish with this scheme? There’s probably a better way to do it.

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

Sidebar

Related Questions

I would like to remove from Mac OS-X taskbar (Dock) the XCode's recent project
In a recent project of mine, i've got up to a good stage where
for a recent project I need to detect file system changes on a mapped
in a recent project I've come really need the lib tre matching library. However
In a recent project, I have to maintain some PHP code. I set up
I'm just adopting Sass in a recent project and am trying to get the
In a recent VB.NET project I adopted the naming conventions I'm used to using
In a recent Xcode 4.3 project template, some @synthesze are declared as: @synthesize window
in my recent asp.net (c#) website project a user needs to login to view
I have created an SVN repository for project but now realise that the files

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.