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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T22:57:39+00:00 2026-06-07T22:57:39+00:00

Recently, I encountered a problem with metaclass calling a derived classmethod. For example, I

  • 0

Recently, I encountered a problem with metaclass calling a derived classmethod.
For example, I get a simple baseclass testA, which has an classmethod do1(a)

class testA(object):

    @classmethod
    def do1(cls, a):
        print "in testA:",cls, a

Then I build a metaclass which actually do nothing but print the cls:

class testMetaA(type):
    def __init__(cls,cname,bases,cdict):
        print "in testMetaA: %s"%cls

Then I could use the metaclass to build a subclass testB, which works as expected:

class testB(testA):

    @classmethod
    def do1(cls, a):
        print "in testB: %s"%cls
        super(testB, cls).do1(a)
    __metaclass__=testMetaA

It will print: in testMetaA: <class '__main__.testB'>; and the testB.do1(a) works as expected:

>>> testB.do1('hello')
in testB: <class '__main__.testB'>
in testA: <class '__main__.testB'> hello

However, if I try to call the classmethod inside the metaclass which contains a “super” as following testMetaB, it will raise an error: NameError: global name 'testC' is not defined.

class testMetaB(type):
    def __init__(cls,cname,bases,cdict):
        print "in testMetaB: %s"%cls
        cls.do1("hello")

class testC(testA):

    @classmethod
    def do1(cls, a):
        print "in testC: %s"%cls
        super(testC, cls).do1(a)
    __metaclass__=testMetaB

I finally find a way to solve it by use super(cls, cls) instead of super(testC, cls):

class testD(testA):

    @classmethod
    def do1(cls, a):
        print "in testD: %s"%cls
        super(cls, cls).do1(a)
    __metaclass__=testMetaB

It will print as:

in testMetaB: <class '__main__.testD'>
in testD: <class '__main__.testD'>
in testA: <class '__main__.testD'> hello

The testD.do1(a) also works as expected:

>>> testD.do1('Well done')
in testD: <class '__main__.testD'>
in testA: <class '__main__.testD'> Well done

Now I am wondering which is the most correct way to use super in a classmethod? Should one always use super(cls,cls) instead of explicitly writing a current class name?

Thanks!

@jsbueno

If some piece of code resorts to tricks like dynamically creating derived classes, that is important – one should not use the class name as first parametere to Super if that name is assigned to another object than the class itself. Instead, cls for class methods, or self.__class__ for instance methods can be passed to Super.

Does this means it is a bad idea to use class name to super in general?

To myself, I usually use super(type(self),self) instead of super(type(self.__class__),self) for normal method. I do not know if there is any major advantage to use self.__class__.
I repeat @jsbueno example like this, here the C use super(type(self),self). So D2() will not change the behavior while the class C gets changed.

>>> class A(object):
    def do(self):
        print "in class A"


>>> class B(A):
    def do(self):
        super(B, self).do()


>>> class C(A):
    def do(self):
        super(type(self),self).do()

>>> D1=B
>>> D2=C
>>> D1().do()
in class A
>>> D2().do()
in class A
>>> class B(A):
    def do(self):
        print "in new class B"


>>> D1().do()

Traceback (most recent call last):
  File "<pyshell#52>", line 1, in <module>
    D1().do()
  File "<pyshell#37>", line 3, in do
    super(B, self).do()
TypeError: super(type, obj): obj must be an instance or subtype of type
>>> class C(A):
    def do(self):
        print "in new class C"
>>> D2().do()
in class A

according to @Don Question’s suggestion, I put the python version here: sys.version= 2.7.2+ (default, Oct 4 2011, 20:06:09) [GCC 4.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-06-07T22:57:41+00:00Added an answer on June 7, 2026 at 10:57 pm

    However, if I try to call the classmethod inside the metaclass which
    contains a “super” as following testMetaB, it will raise an error:
    NameError: global name ‘testC’ is not defined.

    The name TestC will only be bound to the new class after the MetaClass finished it’s work – and that is after returning from it’s __init__ (and before __init__, the __new__) method.

    When we use the “super” call usign the class name as the first parameter, the class name does not appear there magically: it is a (module) global variable, to which the class itself is assigned – in normal circunstances.

    In this case, the name has not been assigned yet – however, as it is a classmethod, yuu have a reference to the class in the cls variable- that is why it works.

    If some piece of code resorts to tricks like dynamically creating derived classes, that is important – one should not use the class name as first parametere to Super if that name is assigned to another object than the class itself. Instead, cls for class methods, or self.__class__ for instance methods can be passed to Super.

    Here is a snippet showing the global name binding for the class name is what super takes:

    >>> class A(object):
    ...   def do(self):
    ...      print "In class A"
    ... 
    >>> class B(A):
    ...   def do(self):
    ...     super(B, self).do()
    ... 
    >>> C = B
    >>> C().do()
    In class A
    >>> class B(object):
    ...   def do(self):
    ...      print "in new class B"
    ... 
    >>> C().do()
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "<stdin>", line 3, in do
    TypeError: super(type, obj): obj must be an instance or subtype of type
    >>> 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I recently encountered a problem with my site. I have a position:fixed div which
I recently encountered a small problem with Storyboards. I have a UINavigationController than has
I recently encountered a problem with my Profile provider: it wouldn't retrieve profiles correctly
I've recently encountered a problem designing a web page. I needed to have rounded
I've recently begun learning C# but have encountered an annoying problem. Every variable I
I recently encountered a scenario in which if a set only contained a single
We recently encountered a problem where a merge somehow led to all the changes
I recently encountered a problem in my production database due to MySQL bug. The
I recently encountered a strange problem on my website. Images with æ ø and
recently I've encountered a problem with IE. I have a function function() { ShowProgress();

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.