0
class My_meta(type):

    def hello(cls):
        print("hey")

class Just_a_class(metaclass=My_meta):
    pass

a = Just_a_class()
a.hello() 

Above code is giving:

AttributeError: 'Just_a_class' object has no attribute 'hello'

Please suggest the changes to make it work. Thanks.

2
  • 5
    A metaclass is not the same as a superclass. Instances don't inherit methods from a metaclass. Commented Sep 27, 2019 at 21:15
  • Why do you believe that an instance of Just_a_class will have that method? Commented Sep 27, 2019 at 22:36

1 Answer 1

1

Methods in a metaclass are inherited by the class object, not class instances. You can call the function this way:

Just_a_class.hello()
// or
a = Just_a_class()
a.__class__.hello()
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for helping!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.