This package tries to emulate the behaviour of syntax proposed in PEP 671 via a decorator

Overview

Late-Bound Arguments

This package tries to emulate the behaviour of syntax proposed in PEP 671 via a decorator.

Usage

Mention the names of the arguments which are to be late-bound in the arguments of the delay decorator, and their default values as strings in the function signature.

Mutable default arguments:

from late_bound_arguments import delay

@delay("my_list")
def foo(my_list="[]"):
    my_list.append(1)
    return my_list

print(foo()) # [1]
print(foo()) # [1]
print(foo([1, 2, 3])) # [1, 2, 3, 1]

Referencing previous arguments:

from late_bound_arguments import delay

@delay("my_list", "n")
def foo(my_list="[1, 2]", n: int = "len(my_list)"):
    return my_list, n


print(foo()) # ([1, 2], 2)
print(foo([1, 2, 3])) # ([1, 2, 3], 3)

Additionally, the function signature is not overwritten, so help(foo) will provide the original signature retaining the default values.

Reasoning

Mutable defaults are often tricky to work with, and may not do what one might naively expect.

For example, consider the following function:

def foo(my_list=[]):
    my_list.append(1)
    return my_list

    
print(foo())
print(foo())

One might expect that this prints [1] twice, but it doesn't. Instead, it prints [1] and [1, 1]. A new list object is not created every time foo is called without providing the b argument: the list is only created once - when the function is defined - and the same list object is used for every call. As a result, if it is mutated in any call, the change is reflected in subsequent calls.

A common workaround for this is using None as a placeholder for the default value, and replacing it inside the function body.

def foo(my_list=None):
    if my_list is None: 
        my_list = []
    ...

In case None is a valid value for the argument, a sentinel object is created beforehand and used as the default instead.

_SENTINEL = object()
def foo(my_list=_SENTINEL):
    if my_list is _SENTINEL: 
        my_list = []
    ...

However, this solution, apart from being unnecessarily verbose, has an additional drawback: help(foo) in a REPL will fail to inform of the default values, and one would have to go through the source code to find the true signature.

Owner
Shakya Majumdar
Shakya Majumdar
Scrapper For Paste.pics

PrntScScrapper Scrapper for Paste.pics If you are bored you can find some random screenshots from prnt.sc Features Saving screenshots Open in Browser

Fareusz 1 Dec 29, 2021
Djangoblog - A blogging site where people can make their accout and write blogs and read other author's blogs

This a blogging site where people can make their accout and write blogs and read other author's blogs.

1 Jan 26, 2022
A simple program which gets a file(CSV/Excel) with tasks and creates different variants

TestMakerProject A simple program which gets a file(CSV/Excel) with tasks and creates different variants The main program is QTengine.py You only have

George 3 Nov 18, 2021
Blender-3D-SH-Dma-plugin - Import and export Sonic Heroes Delta Morph animations (.anm) into Blender 3D

io_scene_sonic_heroes_dma This plugin for Blender 3D allows you to import and ex

Psycrow 3 Mar 22, 2022
Cash in on Expressed Barcode Tags (EBTs) from NGS Sequencing Data with Python

Cash in on Expressed Barcode Tags (EBTs) from NGS Sequencing Data with Python Cashier is a tool developed by Russell Durrett for the analysis and extr

3 Sep 11, 2022
A python package to adjust the bias of probabilistic forecasts/hindcasts using "Mean and Variance Adjustment" method.

Documentation A python package to adjust the bias of probabilistic forecasts/hindcasts using "Mean and Variance Adjustment" method. Read documentation

1 Feb 02, 2022
Reference python implementation of Chia pool operations for pool operators

This repository provides a sample server written in python, which is meant to server as a basis for a Chia Pool. While this is a fully functional implementation, it requires some work in scalability

Chia Network 451 Dec 13, 2022
Python - Aprendendo Python na ByLearn

PYTHON Identação Escopo Pai Escopo filho Escopo neto Variaveis

Italo Rafael 3 May 31, 2022
In this project, we'll be creating a virtual personal assistant for ourselves using our favorite programming language

In this project, we'll be creating a virtual personal assistant for ourselves using our favorite programming language, Python. We can perform several offline as well as online operations using the bo

Ashutosh Krishna 188 Jan 03, 2023
program to store and update pokemons using SQL and Flask

Pokemon SQL and Flask Pokemons api in python. Technologies flask pymysql Description PokeCorp is a company that tracks pokemon and their trainers arou

Sara Hindy Salfer 1 Oct 20, 2021
A set of simple functions to upload and fetch pastes on paste.uploadgram.me

pastegram-py A set of simple functions to upload and fetch pastes on paste.uploadgram.me. API Documentation Methods upload_paste(contents: bytes, file

Uploadgram 3 Sep 13, 2022
An end-to-end encrypted chat

An end-to-end encrypted chat, that allows users to anonymously talk without ip logs, personal info, or need for registration.

Privalise 1 Nov 27, 2021
Strawberry Benchmark With Python

Strawberry benchmarks these benchmarks have been made to compare the performance of dataloaders and joined database queries. How to use You can run th

Doctor 4 Feb 23, 2022
An open-source systems and controls toolbox for Python3

harold A control systems package for Python=3.6. Introduction This package is written with the ambition of providing a full-fledged control systems s

Ilhan Polat 157 Dec 05, 2022
Run python scripts and pass data between multiple python and node processes using this npm module

Run python scripts and pass data between multiple python and node processes using this npm module. process-communication has a event based architecture for interacting with python data and errors ins

Tyler Laceby 2 Aug 06, 2021
A Company Management System For Python

campany-management Getting started To make it easy for you to get started with GitLab, here's a list of recommended next steps. Already a pro? Just ed

hatice akpınar 3 Aug 29, 2022
A python script based on OpenCV-Python, you can automatically hang up the Destiny 2 Throne to get the Dawning Essence.

A python script based on OpenCV-Python, you can automatically hang up the Destiny 2 Throne to get the Dawning Essence.

1 Dec 19, 2021
京东热爱狂欢趴&京东扫码获取cookie

京东热爱狂欢趴 一键完成任务脚本来袭 活动地址: https://wbbny.m.jd.com/babelDiy/Zeus/2s7hhSTbhMgxpGoa9JDnbDzJTaBB/index.html#/home 2021-06-02更新: 1、删除京东星推官 2、更新脚本,修复火爆问题 2021

xoyi 48 Dec 28, 2022
The Open edX platform, the software that powers edX!

This is the core repository of the Open edX software. It includes the LMS (student-facing, delivering courseware), and Studio (course authoring) compo

edX 6.2k Jan 01, 2023
Framework for creating efficient data processing pipelines

Aqueduct Framework for creating efficient data processing pipelines. Contact Feel free to ask questions in telegram t.me/avito-ml Key Features Increas

avito.tech 137 Dec 29, 2022