Skip to content

Commit 56cc151

Browse files
committed
Add one line initializer
Add a one liner to initialize a class and avoid a lot of boilerplate assignments
1 parent 551d947 commit 56cc151

1 file changed

Lines changed: 12 additions & 0 deletions

File tree

one_liners.rst

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,5 +72,17 @@ is a simple example:
7272
print(list(itertools.chain.from_iterable(a_list)))
7373
# Output: [1, 2, 3, 4, 5, 6]
7474
75+
76+
**One Line Constructors**
77+
78+
Avoid a lot of boilerplate assignments when initializing a class
79+
80+
.. code:: python
81+
82+
class A(object):
83+
def __init__(self, a, b, c, d, e, f):
84+
self.__dict__.update({k: v for k, v in locals().items() if k != 'self'})
85+
86+
7587
A couple of more one liners can be found on the `Python
7688
website <https://wiki.python.org/moin/Powerful%20Python%20One-Liners>`__.

0 commit comments

Comments
 (0)