Skip to content

Instantly share code, notes, and snippets.

@carljm
Created June 27, 2012 14:55
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save carljm/3004616 to your computer and use it in GitHub Desktop.
Save carljm/3004616 to your computer and use it in GitHub Desktop.
pure Python implementation of the staticmethod decorator
class StaticMethod(object):
def __init__(self, func):
self.func = func
def __get__(self, obj, cls):
return self.func
def staticmethod(func):
return StaticMethod(func)
@schipiga
Copy link

schipiga commented Sep 5, 2015

import types

class ClassMethod(object):

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

    def __get__(self, obj, objtype=None):
        return types.MethodType(self.func, objtype or type(obj), type)

def class_method(func):
    return ClassMethod(func)

@dogewzy
Copy link

dogewzy commented Jun 20, 2018

why staticmethod need tow params "obj" and "cls"??It seems useless

@schipiga
Copy link

@dogewzy, it's Python descriptors protocol, pls see details in https://docs.python.org/3.7/reference/datamodel.html#object.__get__

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment