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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T12:23:31+00:00 2026-05-26T12:23:31+00:00

After reading the excellent SO post , I tried crafting a module level metaclass:

  • 0

After reading the excellent SO post, I tried crafting a module level metaclass:

def metaclass(future_class_name, future_class_parents, future_class_attrs):
    print "module.__metaclass__"
    future_class_attrs["bar"]="bar"
    return type(future_class_name, future_class_parents, future_class_attrs)

__metaclass__=metaclass


class Foo(object):

    def __init__(self):
        print 'Foo.__init__'

f=Foo()

This doesn’t work (i.e. “module.metaclass” doesn’t get printed) unless I remove the object base class of Foo. How come?

NOTE: I am using Python 2.6.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-05-26T12:23:32+00:00Added an answer on May 26, 2026 at 12:23 pm

    Inheriting from object automatically brings the type metaclass along with it. This overrides your module level __metaclass__ specification.

    If the metaclass is specified at the class level, then object won’t override it:

    def metaclass(future_class_name, future_class_parents, future_class_attrs):
        print "module.__metaclass__"
        future_class_attrs["bar"]="bar"
        return type(future_class_name, future_class_parents, future_class_attrs)
    
    class Foo(object):
        __metaclass__ = metaclass
    
        def __init__(self):
            print 'Foo.__init__'
    
    f=Foo()
    

    See http://docs.python.org/reference/datamodel.html?highlight=metaclass#customizing-class-creation

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

Sidebar

Related Questions

After reading the most excellent book Head First Design Patterns, I began proselytizing to
After reading Jeff's blog post on Protecting Your Cookies: HttpOnly . I'd like to
after reading few post in this forum on this topic i had better understanding
After reading this answer I've tried to play with this nice feature by myself
After reading this post I was wondering what would be the best way to
After reading Steve Sandersons post on swf upload. http://blog.stevensanderson.com/2008/11/24/jquery-ajax-uploader-plugin-with-progress-bar/ I have implemented the swf
After reading the Head First Design Patterns book and using a number of other
After reading this question , I was reminded of when I was taught Java
After reading a bit more about how Gnutella and other P2P networks function, I
After reading Practical Common Lisp I finally understood what the big deal about macros

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.