Created
November 3, 2017 15:19
-
-
Save harryscholes/383b58bb7b428f8e7334fb2969e7a57a to your computer and use it in GitHub Desktop.
Boilerplate class definition in Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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