-
-
Save danbornside/5131325 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 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