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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T19:22:37+00:00 2026-06-17T19:22:37+00:00

From this answer to what is a metaclass? I got this: You write class

  • 0

From this answer to “what is a metaclass?” I got this:

You write class Foo(object) first, but the class object Foo is not created in memory yet.

Python will look for metaclass in the class definition. If it finds it, it will use it to create the object class Foo. If it doesn’t, it will use type to create the class.

Having tested it, it seems that the attributes of the class are instantiated before the constructor of the class is run. What am I misunderstanding?

Test code:

class meta(type):

    def __init__(cls, name, bases, dic):
        type.__init__(cls, name, bases, dic)

        print hasattr(cls, "a")
        cls.a = "1"


class A(object):
    a = "a"
    __metaclass__ = meta


class B(object):
    __metaclass__ = meta


class C(object):
    __metaclass__ = meta
    a = "a"


print A.a
print B.a
print C.a

Output:

True
False
True
1
1
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-17T19:22:38+00:00Added an answer on June 17, 2026 at 7:22 pm

    The class body is run before the class is constructed, yes.

    The body of the class provides a temporary namespace, and all local names in that namespace are given as a dictionary to construct the class object, together with the base classes and a name for the class.

    You can do this with the type() constructor too:

    >>> Foo = type('Foo', (), {'a': 1})
    >>> Foo.a
    1
    

    The class body is basically executed as a function, with the local namespace of that function being used to create the class attributes, the 3rd argument to type() above.

    In python 3 you have a little more influence on that process with the __prepare__ hook on a metaclass. __prepare__ should be a class method that returns a initial namespace for the class body; use it to inject extra names into the generated class body before the class body is executed:

    class MyMeta(type):
       @classmethod
       def __prepare__(mcl, name, bases):
           return {'a': 1}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This question arose from the discussion in the comments of this answer . First,
Following on from this answer , it seems these constructors: template<class U, class V>
I've used validator errors customization example from this answer: https://stackoverflow.com/a/4881030/822947 , but I need
From this answer in stack overflow and the sample project referred there, i got
I was able to find the documentation on PageEditor from this answer . But
I got the country iso by the code from this answer : TelephonyManager tm
From this answer: When is a C++ terminate handler the Right Thing(TM)? It would
I'm having trouble figuring out how to implement the code from this answer. Android:
I am using the accepted answer from this question . The relevant code is
From this question & the answers - What is the correct answer for cout

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.