Created
January 12, 2013 19:32
-
-
Save Nurdok/4520107 to your computer and use it in GitHub Desktop.
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
"""TODO: A docstring""" | |
class Bean(type): | |
def __init__(cls, name, bases, dct): | |
def init(self, *args, **kwargs): | |
for attr, arg in zip(cls._attrs, args): | |
setattr(self, attr, arg) | |
for name, value in kwargs.iteritems(): | |
setattr(self, name, value) | |
undefined = [] | |
for attr in cls._attrs: | |
try: | |
getattr(self, attr) | |
except AttributeError: | |
undefined.append(attr) | |
if undefined: | |
print undefined | |
raise ValueError('The following arguments are missing: {0}' | |
.format(', '.join(undefined))) | |
setattr(cls, '__init__', init) | |
class Point(object): | |
__metaclass__ = Bean | |
_attrs = ('x', 'y') | |
p = Point(2, 5) | |
print p |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment