[Python] How to pass any object as a classmethod's first argument - circumvent injection of...

Discussão em 'Python' iniciado por Stack, Junho 14, 2025.

  1. Stack

    Stack Membro Participativo

    Passing a class, or a totally different object, as the first argument to method is easy:

    class Foo:
    def method(self): ...

    Foo.method(object()) # pass anything to self


    I wonder, is this possible with classmethods as well? I assume it is, but how can it be done?

    class Foo:
    @classmethod
    def cls_method(cls): ...

    Foo.cls_method # how to pass cls=object


    Related: hybrid / class_or_instance_method decorator / descriptor, but that is not a setting I am interested in.

    Continue reading...

Compartilhe esta Página