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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T13:56:29+00:00 2026-05-24T13:56:29+00:00

I read What is a metaclass in Python? and I tried to replicate the

  • 0

I read What is a metaclass in Python?

and I tried to replicate the upper metaclass from the example and found that this doesn’t work in all cases:

def upper(cls_name, cls_parents, cls_attr):                                     
    """ Make all class attributes uppper case """                               
    attrs = ((name, value) for name, value in cls_attr.items()                  
            if not name.startswith('__'))                                       
    upper_atts = dict((name.upper(), value) for name, value in attrs)           
    return type(cls_name, cls_parents, upper_atts)                              

__metaclass__ = upper #Module level
class Foo:                                                                      
    bar = 1                                                                     
f =  Foo()
print(f.BAR) #works in python2.6

The above fails (with an attribute error) in python3 which I think is natural because all classes in python3 already have object as their parent and metaclass resolution goes into the object class.

The question:

How do I make a module level metaclass in python3?

  • 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-24T13:56:31+00:00Added an answer on May 24, 2026 at 1:56 pm

    The module level metaclass isn’t really “module level”, it has to do with how class initialization worked. The class creation would look for the variable "__metaclass__" when creating the class, and if it wasn’t in the local environment it would look in the global. Hence, if you had a “module level” __metaclass__ that would be used for every class afterwards, unless they had explicit metaclasses.

    In Python 3, you instead specify the metaclass with a metaclass= in the class definition. Hence there is no module level metaclasses.

    So what do you do? Easy: You specify it explicitly for each class.

    It’s really not much extra work, and you can even do it with a nice regexp search and replace if you really have hundreds of classes and don’t want to do it manually.

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

Sidebar

Related Questions

I have read posts like these: What is a metaclass in Python? What are
I read in this article that a company has created a software capable of
Read that the following code is an example of unsafe construction as it allows
I read about __packed__ from here and, I understood that when __packed__ is used
I read how this can be made to work using forward declarations. class A
Read in some blog that GC in Android happens on main(UI) thread, this may
I read this question's answers that explain the order of the LINQ to objects
Read about Server push here . I want to push data to client from
Read on before you say this is a duplicate, it's not. (as far as
I read this PHP RegEx page , but either I'm missing something, misreading something,

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.