Created
March 11, 2013 00:51
-
-
Save anonymous/5131239 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
class MyMutableClass(object): | |
def __init__(self, foo, bar): | |
"This is a chore" | |
self.foo, self.bar = foo, bar | |
def mutateThing(self, baz=None, quux=None, wibble=None, wobble=None, wubble=None) | |
"I hope i didnt miss any arguments there" | |
if baz is none: | |
self.baz = baz | |
if quux is none: | |
self.quux = quux | |
if wibble is none: | |
self.wibble = wibble | |
if wobble is none: | |
self.wobble = wobble | |
if wubble is none: | |
self.wobble = wubble | |
from collections import namedtuple | |
Mutation = namedtuple('Mutation', 'baz quux wibble wobble wubble') | |
class Nicer(object): | |
def __init__(self, foo, bar): | |
kwargs = vars() | |
kwargs.pop('self') | |
for attr, value in kwargs.iteritems() | |
setattr(self, attr, value) | |
def mutate(self, mutation): | |
if not isinstance(mutation, Mutation): | |
raise ValueError("mutation must be an instance of Mutation") | |
for attr, value in mutation._asdict().iteritems() | |
if value is not None: | |
setattr(self, attr, value) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment