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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T17:11:52+00:00 2026-05-13T17:11:52+00:00

I was wondering what happens to methods declared on a metaclass. I expected that

  • 0

I was wondering what happens to methods declared on a metaclass. I expected that if you declare a method on a metaclass, it will end up being a classmethod, however, the behavior is different. Example

>>> class A(object):
...     @classmethod
...     def foo(cls):
...         print "foo"
... 
>>> a=A()
>>> a.foo()
foo
>>> A.foo()
foo

However, if I try to define a metaclass and give it a method foo, it seems to work the same for the class, not for the instance.

>>> class Meta(type): 
...     def foo(self): 
...         print "foo"
... 
>>> class A(object):
...     __metaclass__=Meta
...     def __init__(self):
...         print "hello"
... 
>>> 
>>> a=A()
hello
>>> A.foo()
foo
>>> a.foo()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'A' object has no attribute 'foo'

What’s going on here exactly ?

edit: bumping the question

  • 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-13T17:11:53+00:00Added an answer on May 13, 2026 at 5:11 pm

    You raise a good point.

    Here is a good referenceto get a better understanding of the relations between objects, classes and metaclasses:

    I also find this reference on descriptors to be quite enlightening about the mechanism of look-up in python.

    But I can’t say I understand why a.foo fails when A.foo succeeds. It seems that when you look up an attribute of an object, and python does not find it there, it does not exactly look up the attribute in the class, because if it did, it would find A.foo.

    EDIT:

    Oh! I think I got it. It is due to how inheritance works. If you consider the schema provided by the above link, it looks like this:

    alt text

    Schematically, it boils down to:

    type -- object
      |       |
    Meta --   A  -- a
    

    Going left means going to the class of a given instance. Going up means going to the parent.

    Now the inheritance mechanism makes the look-up mechanism make a right turn in the schema above. It goes a → A → object. It must do so in order to follow the inheritance rule! To make it clear, the search path is:

     object
       ^
       |
       A  <-- a
    

    Then, clearly, the attribute foo will not be found.

    When you lookup for the attribute foo in A, however, it is found, because the lookup path is:

    type
      ^
      |       
    Meta <--   A 
    

    It all makes sense when one thinks of how inheritance works.

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

Sidebar

Related Questions

I'm wondering if anyone knows how this happens? My website is down, but every
Im' wondering if this is possible: I've successfully used __set() magic method to set
Apart from Dll concept that provides ability of loading/unloading methods or functions at run-time,
I was wondering how to apply a match and case to my act() method.
I still learning Ruby on Rails, but have been wondering what happens if I
Wondering if anyone has gotten the infamous database is locked error from Trac and
Wondering if there is any Text to Speech software available as a plug in
Wondering if I need to do something in my swf to be able to
Wondering if anybody out there has any success in using the JDEdwards XMLInterop functionality.
Wondering how to open many new windows with Javascript. I have found plenty of

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.