Last active
December 17, 2015 00:09
-
-
Save nathania/5518356 to your computer and use it in GitHub Desktop.
Set a variable with the same name as a parameter equal to the argument. Became very tired of writing self.x = x.
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 Compact_assignment_from_pparams(object): | |
def __init__(self, p1, p2, p3, p4, p5, p6): | |
for key, value in locals().items(): | |
setattr(self, key, value) | |
class Compact_assignment_from_kwargs(object): | |
def __init__(self, **kwargs): | |
for key, value in kwargs.items(): | |
setattr(self, key, value) | |
class Lots_of_members_to_set(object): | |
def __init__(self, p1, p2, p3, p4, p5, p6): | |
self.p1 = p1 | |
self.p2 = p2 | |
self.p3 = p3 | |
self.p4 = p4 | |
self.p5 = p5 | |
self.p6 = p6 | |
# Caution: This is a terrible idea if your interface is subject to change. | |
# Intended as a convenience when testing ideas, scripting, etc. | |
# TODO: Look at the source code for locals(), make sure that when called | |
# from within a function it only returns the parameters passed to the | |
# function, even if there are variables created w/in the function. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Use with caution. This seems to works anywhere within a function, even if other variables have been created in the same scope -- not sure why just yet.