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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T10:50:01+00:00 2026-06-12T10:50:01+00:00

I have a case where my class has a custom metaclass, which calls a

  • 0

I have a case where my class has a custom metaclass, which calls a class method of the class when creating it, something like:

class Metaclass(type):
    def __new__(cls, name, bases, attrs):
        ...
        new_class = super(Metaclass, cls).__new__(cls, name, bases, attrs)
        ...
        new_class.get_fields() # do something
        ...
        return new_class

class FooBar(object):
    __metaclass__ = Metaclass

    @classmethod
    def get_fields(cls):
        ...

(Example of such code is in Tastypie.)

The problem is if I want to do:

class NewBar(FooBar):
    @classmethod
    def get_fields(cls):
        super(NewBar, cls).get_fields()
        ...

This does not work because NewBar is not yet created at the point super is invoked (program flow is still in metaclass). So, is there any workaround?

I know that probably get_fields method could become a method of metaclass, but this would make inheritance much harder to implement (you would have to define both new metaclass and class itself, not nice to developers wanting to extend this classes).

(Python 2.7.)

  • 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-12T10:50:03+00:00Added an answer on June 12, 2026 at 10:50 am

    If NewBar can be unavailable when get_fields is invoked, you can still find it in the MRO of cls:

    @classmethod
    def get_fields(cls):
        # we can get invoked before NewBar is available in globals,
        # so get NewBar from cls.__mro__
        NewBar = next(c for c in cls.__mro__
                      if c.__module__ == __name__ and c.__name__ == 'NewBar')
        super(NewBar, cls).get_fields()
        ...
    

    Although this code looks funny, it works correctly and is significantly simpler than the alternatives proposed in the question. While most calls to super with a non-constant first argument (such as unqualified super(cls, cls)) are incorrect and break inheritance, this one is safe because the generator expression is nothing but an unconventional way to get a hold of NewBar.

    When looking for the clas in the MRO we check for both class and module name (available as __name__, as pointed out by Mitar) to avoid a false positive if othermodule.NewBar inherits from thismodule.NewBar.

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

Sidebar

Related Questions

I have an Array list of a custom class which is essentially has two
i have a List<my_Custom_Class> and List<string> . My custom class has various items in
I have a COM class (developed in C++, just in case it matters), which
I have an NSArray formed with objects of a custom class. The class has
I have a custom class, FavoriteColor that has three properties, R , G and
I have a custom TagField form field. class TagField(forms.CharField): def __init__(self, *args, **kwargs): super(TagField,
I have a custom class in Obj-C called RouteManager which contains an array of
I have a custom class I use frequently on my projects. This class has
I have a class which has the following constructor public DelayCompositeDesigner(DelayComposite CompositeObject) { InitializeComponent();
I have a custom view class, which receives the mouse events. I do not

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.