Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How is Django's classonlymethod useful? [duplicate]

Tags:

python

django

The source code is below:

class classonlymethod(classmethod):
    def __get__(self, instance, owner):
        if instance is not None:
            raise AttributeError("This method is available only on the view class.")
        return super(classonlymethod, self).__get__(instance, owner)

Though I can see that classonlymethod can only be called on the class and not on an instance unlike classmethod of python, why do we need such a "restriction"?

Not much on the www regarding classonlymethod and any layman examples appreciated as always.

like image 613
masterpiece Avatar asked Oct 26 '25 09:10

masterpiece


1 Answers

It is used internally on class-based views' as_view to give a descriptive error message to people who try to call it on an instance.

I'm not sure who first decided it's mandatory.

like image 122
Pavel Anossov Avatar answered Oct 28 '25 23:10

Pavel Anossov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!