Hello Python Community,
I’d like to propose a new syntax feature that would reduce verbosity when constructing dictionaries and passing function arguments, inspired by JavaScript’s ES6 shorthand property syntax.
Motivation
Python developers frequently write code where variable names match dictionary keys or parameter names, resulting in redundant patterns:
Dictionary construction:
param1 = 10
param2 = 20
my_dict = {'param1': param1, 'param2': param2} # repetitive
Function calls:
x = 10
y = 20
def my_func(x=None, y=None, z=5):
print(x, y, z)
my_func(x=x, y=y) # repetitive
This becomes particularly burdensome in data science workflows, configuration management, and API interactions where dozens of parameters are common.
Proposed Syntax
Allow variable names to automatically become dictionary keys when needed:
Dictionary shorthand:
param1 = 10
param2 = 20
my_dict = dict(param1, param2)
# Equivalent to: {'param1': param1, 'param2': param2}
Function argument shorthand:
a = 1
b = 2
param1 = 10
param2 = 20
def my_func(a, b, param1=None, param2=None):
print(a, b, param1, param2)
my_func(a, b, **dict(param1, param2))
# Output: 1 2 10 20
Real-World Use Cases
This would significantly improve code clarity in scenarios like:
-
Machine learning: Parameter grids for sklearn’s
GridSearchCV, model configurations -
API clients: Request payloads where parameter names mirror local variables
-
Logging: Structured context with multiple variables
-
Configuration: Generating config dictionaries from local state
-
Database queries: Parameter binding with named placeholders
Example:
learning_rate = 0.01
batch_size = 32
epochs = 100
model.train(**dict(learning_rate, batch_size, epochs))
Instead of:
model.train(
learning_rate= learning_rate,
batch_size= batch_size,
epochs= epochs
)
Benefits
-
Reduced verbosity: Less repetitive code without sacrificing clarity
-
Explicit references: Unlike
locals()orvars(), you explicitly choose which variables to include -
Modern alignment: Brings Python closer to contemporary language features (ES6, Rust)
-
Practical value: Especially beneficial for data-heavy and configuration-intensive applications
Backward Compatibility
This would be a purely additive change:
-
Existing
dict()behavior with keyword arguments remains unchanged -
Set literal syntax
{param}is unaffected -
No breaking changes to existing code
-
Clear distinction:
dict(x=x)(current) vsdict(x)(proposed)
Potential Challenges
I recognize several areas requiring careful consideration:
-
Scope resolution: How to handle non-local variables or closures
-
Name conflicts: Behavior when variable names collide
-
Parser complexity: Implementation effort and maintenance burden
-
Python philosophy: Whether this aligns with “explicit is better than implicit”
Existing Workarounds and Their Limitations
Current approaches have drawbacks:
-
locals(): Captures all local variables (too broad, unpredictable) -
vars(): Requires an object reference -
Manual construction: Verbose (the problem we’re solving)
-
Dict comprehensions: Still require repetition:
{k: v for k, v in [('x', x), ('y', y)]}
Questions for the Community
I’d greatly appreciate feedback on:
-
Would this feature address pain points in your codebases?
-
Are there implementation challenges I haven’t considered?
-
Should we explore alternative syntax approaches?
-
Are there similar past discussions or PEPs I should review?
Thank you for considering this proposal. I look forward to your insights.
Best regards,
Sadegh Khajepour