β² 16 βΌ Avoiding boilerplate by using immutable default arguments (vulwsztyn.codeberg.page) submitted 1 year ago by Vulwsztyn@programming.dev to c/python@programming.dev 8 comments fedilink hide all child comments Hi, I recently realised one can use immutable default arguments to avoid a chain of: def append_to(element, to=None): if to is None: to = [] at the beginning of each function with default argument for set, list, or dict.
[β] Narann@jlai.lu 4 points 1 year ago (7 children) This is the way you're supposed to write it in Python. It is something you get used to, yet I think it's sad. permalink fedilink source hideshow 7 child comments replies: [β] m_f@discuss.online 5 points 1 year ago (5 children) You can use mutable default arguments now with a new syntax: https://peps.python.org/pep-0671/ def bisect_right(a, x, lo=0, hi=>len(a), *, key=None): def connect(timeout=>default_timeout): def add_item(item, target=>[]): def format_time(fmt, time_t=>time.time()): permalink fedilink source parent hideshow 5 child comments replies: [β] Doccool@lemmy.world 4 points 1 year ago Oh wow! This would be great I really hope it's accepted and implemented, makes a lot of sense! permalink fedilink source parent [β] Narann@jlai.lu 3 points 1 year ago (1 child) Does not seems to work on 3.12: Python 3.12.11 (main, Jun 29 2025, 16:18:35) [MSC v.1944 64 bit (AMD64)] on win32 >>> def toto(tata=>[]): File "<stdin>", line 1 def toto(tata=>[]): ^ SyntaxError: invalid syntax permalink fedilink source parent hideshow 1 child comment replies: [β] logging_strict@programming.dev 2 points 1 year ago* Upvote for the sanity check. As the OP mentioned, this is a proposed/draft feature that may or may not ever happen. With these kinda posts, should start a betting pool. To put money down on whether this feature sees the light of day within an agreed upon fixed time frame. permalink fedilink source parent [β] Vulwsztyn@programming.dev [S] 3 points 1 year ago This is only a Draft for now though permalink fedilink source parent [β] Vulwsztyn@programming.dev [S] 2 points 1 year ago Thank you permalink fedilink source parent [β] Vulwsztyn@programming.dev [S] 1 point 1 year ago Yeah, but you can also write it like I proposed which is less boilerplaty and gives the same advantages permalink fedilink source parent
[β] m_f@discuss.online 5 points 1 year ago (5 children) You can use mutable default arguments now with a new syntax: https://peps.python.org/pep-0671/ def bisect_right(a, x, lo=0, hi=>len(a), *, key=None): def connect(timeout=>default_timeout): def add_item(item, target=>[]): def format_time(fmt, time_t=>time.time()): permalink fedilink source parent hideshow 5 child comments replies: [β] Doccool@lemmy.world 4 points 1 year ago Oh wow! This would be great I really hope it's accepted and implemented, makes a lot of sense! permalink fedilink source parent [β] Narann@jlai.lu 3 points 1 year ago (1 child) Does not seems to work on 3.12: Python 3.12.11 (main, Jun 29 2025, 16:18:35) [MSC v.1944 64 bit (AMD64)] on win32 >>> def toto(tata=>[]): File "<stdin>", line 1 def toto(tata=>[]): ^ SyntaxError: invalid syntax permalink fedilink source parent hideshow 1 child comment replies: [β] logging_strict@programming.dev 2 points 1 year ago* Upvote for the sanity check. As the OP mentioned, this is a proposed/draft feature that may or may not ever happen. With these kinda posts, should start a betting pool. To put money down on whether this feature sees the light of day within an agreed upon fixed time frame. permalink fedilink source parent [β] Vulwsztyn@programming.dev [S] 3 points 1 year ago This is only a Draft for now though permalink fedilink source parent [β] Vulwsztyn@programming.dev [S] 2 points 1 year ago Thank you permalink fedilink source parent
[β] Doccool@lemmy.world 4 points 1 year ago Oh wow! This would be great I really hope it's accepted and implemented, makes a lot of sense! permalink fedilink source parent
[β] Narann@jlai.lu 3 points 1 year ago (1 child) Does not seems to work on 3.12: Python 3.12.11 (main, Jun 29 2025, 16:18:35) [MSC v.1944 64 bit (AMD64)] on win32 >>> def toto(tata=>[]): File "<stdin>", line 1 def toto(tata=>[]): ^ SyntaxError: invalid syntax permalink fedilink source parent hideshow 1 child comment replies: [β] logging_strict@programming.dev 2 points 1 year ago* Upvote for the sanity check. As the OP mentioned, this is a proposed/draft feature that may or may not ever happen. With these kinda posts, should start a betting pool. To put money down on whether this feature sees the light of day within an agreed upon fixed time frame. permalink fedilink source parent
[β] logging_strict@programming.dev 2 points 1 year ago* Upvote for the sanity check. As the OP mentioned, this is a proposed/draft feature that may or may not ever happen. With these kinda posts, should start a betting pool. To put money down on whether this feature sees the light of day within an agreed upon fixed time frame. permalink fedilink source parent
[β] Vulwsztyn@programming.dev [S] 3 points 1 year ago This is only a Draft for now though permalink fedilink source parent
[β] Vulwsztyn@programming.dev [S] 1 point 1 year ago Yeah, but you can also write it like I proposed which is less boilerplaty and gives the same advantages permalink fedilink source parent
all 8 comments