Created
June 21, 2019 02:17
-
-
Save oversider-kosma/ddc933f37561507b071fecfd8c625783 to your computer and use it in GitHub Desktop.
When to lazy to do simple assigments.
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
#!/usr/bin/env python2 | |
class SomeClass(object): | |
def __init__(self, an_attribute, another_attribute, attr_with_name_to_hard_to_write, | |
hate_to_write_stupid_identical_assignments, | |
so_lazy_to_write_self_many_times, no_dataclasses_in_py2, | |
what_can_we_do_with_a_drunken_sailor, here_is_a_solution, | |
kak_tebe_takoe_Elon_Musk): | |
(lambda l: [setattr(self, attr, l[attr]) for attr in | |
filter(lambda x: x != 'self', | |
getattr(self.__init__, 'func_code', getattr(self.__init__, '__code__')).co_varnames)])(locals()) | |
# TODO: upgrade to mixin or decorator | |
instance = SomeClass(*range(9)) | |
expected_attrs = {'an_attribute', 'another_attribute', | |
'attr_with_name_to_hard_to_write', | |
'hate_to_write_stupid_identical_assignments', | |
'here_is_a_solution', 'kak_tebe_takoe_Elon_Musk', | |
'no_dataclasses_in_py2', | |
'so_lazy_to_write_self_many_times', | |
'what_can_we_do_with_a_drunken_sailor'} | |
assert set(filter(lambda x: not x.startswith('__'), dir(instance))) == set(expected_attrs) | |
assert instance.an_attribute == 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment