Skip to content

Instantly share code, notes, and snippets.

@harryscholes
Created November 3, 2017 15:19
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save harryscholes/383b58bb7b428f8e7334fb2969e7a57a to your computer and use it in GitHub Desktop.
Save harryscholes/383b58bb7b428f8e7334fb2969e7a57a to your computer and use it in GitHub Desktop.
Boilerplate class definition in Python
class Meta(type):
"""
Flexible metaclass for defining useful decorator functions.
"""
def __new__(cls, clsname, bases, clsdict):
clsobj = super().__new__(cls, clsname, bases, clsdict)
return clsobj
class Base(object, metaclass=Meta):
"""
Base class.
Args:
*args (list): list of arguments
**kwargs (dict): dict of keyword arguments
Attributes:
self
"""
def __init__(self, *args, **kwargs):
allowed_keys = set([])
self.__dict__.update((k, False) for k in allowed_keys)
self.__dict__.update((k, v) for k, v in kwargs.items() if k in allowed_keys)
def foo(self):
"""
Function.
"""
pass
class Derived(Base):
"""
Derived class.
Args:
*args (list): list of arguments
**kwargs (dict): dict of keyword arguments
Attributes:
self
"""
def __init__(self, *args, **kwargs):
allowed_keys = set([])
self.__dict__.update((k, False) for k in allowed_keys)
self.__dict__.update((k, v) for k, v in kwargs.items() if k in allowed_keys)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment