What are the differences between a `classmethod` and a metaclass method?
Asked Answered
R

4

18

In Python, I can create a class method using the @classmethod decorator:

>>> class C:
...     @classmethod
...     def f(cls):
...             print(f'f called with cls={cls}')
...
>>> C.f()
f called with cls=<class '__main__.C'>

Alternatively, I can use a normal (instance) method on a metaclass:

>>> class M(type):
...     def f(cls):
...             print(f'f called with cls={cls}')
...
>>> class C(metaclass=M):
...     pass
...
>>> C.f()
f called with cls=<class '__main__.C'>

As shown by the output of C.f(), these two approaches provide similar functionality.

What are the differences between using @classmethod and using a normal method on a metaclass?

Rozele answered 15/12, 2019 at 6:46 Comment(0)
O
9

As classes are instances of a metaclass, it is not unexpected that an "instance method" on the metaclass will behave like a classmethod.

However, yes, there are differences - and some of them are more than semantic:

  1. The most important difference is that a method in the metaclass is not "visible" from a class instance. That happens because the attribute lookup in Python (in a simplified way - descriptors may take precedence) search for an attribute in the instance - if it is not present in the instance, Python then looks in that instance's class, and then the search continues on the superclasses of the class, but not on the classes of the class. The Python stdlib make use of this feature in the abc.ABCMeta.register method. That feature can be used for good, as methods related with the class themselves are free to be re-used as instance attributes without any conflict (but a method would still conflict).
  2. Another difference, though obvious, is that a method declared in the metaclass can be available in several classes, not otherwise related - if you have different class hierarchies, not related at all in what they deal with, but want some common functionality for all classes, you'd have to come up with a mixin class, that would have to be included as base in both hierarchies (say for including all classes in an application registry). (NB. the mixin may sometimes be a better call than a metaclass)
  3. A classmethod is a specialized "classmethod" object, while a method in the metaclass is an ordinary function.

So, it happens that the mechanism that classmethods use is the "descriptor protocol". While normal functions feature a __get__ method that will insert the self argument when they are retrieved from an instance, and leave that argument empty when retrieved from a class, a classmethod object have a different __get__, that will insert the class itself (the "owner") as the first parameter in both situations.

This makes no practical differences most of the time, but if you want access to the method as a function, for purposes of adding dynamically adding decorator to it, or any other, for a method in the metaclass meta.method retrieves the function, ready to be used, while you have to use cls.my_classmethod.__func__ to retrieve it from a classmethod (and then you have to create another classmethod object and assign it back, if you do some wrapping).

Basically, these are the 2 examples:


class M1(type):
    def clsmethod1(cls):
        pass

class CLS1(metaclass=M1):
    pass

def runtime_wrap(cls, method_name, wrapper):
    mcls = type(cls)
    setattr(mcls, method_name,  wrapper(getatttr(mcls, method_name)))

def wrapper(classmethod):
    def new_method(cls):
        print("wrapper called")
        return classmethod(cls)
    return new_method

runtime_wrap(cls1, "clsmethod1", wrapper)

class CLS2:
    @classmethod
    def classmethod2(cls):
        pass

 def runtime_wrap2(cls, method_name, wrapper):
    setattr(cls, method_name,  classmethod(
                wrapper(getatttr(cls, method_name).__func__)
        )
    )

runtime_wrap2(cls1, "clsmethod1", wrapper)

In other words: apart from the important difference that a method defined in the metaclass is visible from the instance and a classmethod object do not, the other differences, at runtime will seem obscure and meaningless - but that happens because the language does not need to go out of its way with special rules for classmethods: Both ways of declaring a classmethod are possible, as a consequence from the language design - one, for the fact that a class is itself an object, and another, as a possibility among many, of the use of the descriptor protocol which allows one to specialize attribute access in an instance and in a class:

The classmethod builtin is defined in native code, but it could just be coded in pure python and would work in the exact same way. The 5 line class bellow can be used as a classmethod decorator with no runtime differences to the built-in @classmethod" at all (though distinguishable through introspection such as calls toisinstance, and evenrepr` of course):


class myclassmethod:
    def __init__(self, func):
        self.__func__ = func
    def __get__(self, instance, owner):
        return lambda *args, **kw: self.__func__(owner, *args, **kw)

And, beyond methods, it is interesting to keep in mind that specialized attributes such as a @property on the metaclass will work as specialized class attributes, just the same, with no surprising behavior at all.

Officeholder answered 19/12, 2019 at 15:4 Comment(0)
S
2

When you phrase it like you did in the question, the @classmethod and metaclasses may look similar but they have rather different purposes. The class that is injected in the @classmethod's argument is usually used for constructing an instance (i.e. an alternative constructor). On the other hand, the metaclasses are usually used to modify the class itself (e.g. like what Django does with its models DSL).

That is not to say that you can't modify the class inside a classmethod. But then the question becomes why didn't you define the class in the way you want to modify it in the first place? If not, it might suggest a refactor to use multiple classes.

Let's expand the first example a bit.

class C:
    @classmethod
    def f(cls):
        print(f'f called with cls={cls}')

Borrowing from the Python docs, the above will expand to something like the following:

class ClassMethod(object):
    "Emulate PyClassMethod_Type() in Objects/funcobject.c"

    def __init__(self, f):
        self.f = f

    def __get__(self, obj, klass=None):
        if klass is None:
            klass = type(obj)
        def newfunc(*args):
            return self.f(klass, *args)
        return newfunc

class C:
    def f(cls):
        print(f'f called with cls={cls}')
    f = ClassMethod(f)

Note how __get__ can take either an instance or the class (or both), and thus you can do both C.f and C().f. This is unlike the metaclass example you give which will throw an AttributeError for C().f.

Moreover, in the metaclass example, f does not exist in C.__dict__. When looking up the attribute f with C.f, the interpreter looks at C.__dict__ and then after failing to find, looks at type(C).__dict__ (which is M.__dict__). This may matter if you want the flexibility to override f in C, although I doubt this will ever be of practical use.

Sidonius answered 15/12, 2019 at 11:42 Comment(0)
M
0

In your example, the difference would be in some other classes that will have M set as their metaclass.

class M(type):
    def f(cls):
        pass

class C(metaclass=M):
    pass

class C2(metaclass=M):
    pass

C.f()
C2.f()
class M(type):
     pass

class C(metaclass=M):
     @classmethod
     def f(cls):
        pass

class C2(metaclass=M):
    pass

C.f()
# C2 does not have 'f'

Here is more on metaclasses What are some (concrete) use-cases for metaclasses?

Mucro answered 15/12, 2019 at 7:38 Comment(0)
E
0

Both @classmethod and Metaclass are different.

Everything in python is an object. Every thing means every thing.

What is Metaclass ?

As said every thing is an object. Classes are also objects in fact classes are instances of other mysterious objects formally called as meta-classes. Default metaclass in python is "type" if not specified

By default all classes defined are instances of type.

Classes are instances of Meta-Classes

Few important points are to understand metioned behaviour

  • As classes are instances of meta classes.
  • Like every instantiated object, like objects(instances) get their attributes from class. Class will get it's attributes from Meta-Class

Consider Following Code

class Meta(type):
    def foo(self):
        print(f'foo is called self={self}')
        print('{} is instance of {}: {}'.format(self, Meta, isinstance(self, Meta)))

class C(metaclass=Meta):
    pass

C.foo()

Where,

  • class C is instance of class Meta
  • "class C" is class object which is instance of "class Meta"
  • Like any other object(instance) "class C" has access it's attributes/methods defined in it's class "class Meta"
  • So, decoding "C.foo()" . "C" is instance of "Meta" and "foo" is method calling through instance of "Meta" which is "C".
  • First argument of method "foo" is reference to instance not class unlike "classmethod"

We can verify as if "class C" is instance of "Class Meta

  isinstance(C, Meta)

What is classmethod?

Python methods are said to be bound. As python imposes the restriction that method has to be invoked with instance only. Sometimes we might want to invoke methods directly through class without any instance (much like static members in java) with out having to create any instance.By default instance is required to call method. As a workaround python provides built-in function classmethod to bind given method to class instead of instance.

As class methods are bound to class. It takes at least one argument which is reference to class itself instead of instance (self)

if built-in function/decorator classmethod is used. First argument will be reference to class instead of instance

class ClassMethodDemo:
    @classmethod
    def foo(cls):
        print(f'cls is ClassMethodDemo: {cls is ClassMethodDemo}')

As we have used "classmethod" we call method "foo" without creating any instance as follows

ClassMethodDemo.foo()

Above method call will return True. Since first argument cls is indeed reference to "ClassMethodDemo"

Summary:

  • Classmethod's receive first argument which is "a reference to class(traditionally referred as cls) itself"
  • Methods of meta-classes are not classmethods. Methods of Meta-classes receive first argument which is "a reference to instance(traditionally referred as self) not class"
Eastwardly answered 24/12, 2019 at 20:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.