Skip to content

Instantly share code, notes, and snippets.

@danbornside
Forked from anonymous/gist:5131239
Last active December 14, 2015 18:39
Show Gist options
  • Save danbornside/5131325 to your computer and use it in GitHub Desktop.
Save danbornside/5131325 to your computer and use it in GitHub Desktop.
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 not None:
self.baz = baz
if quux is not None:
self.quux = quux
if wibble is not None:
self.wibble = wibble
if wobble is not None:
self.wobble = wobble
if wubble is not 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