Python Forum
def function default parameter expressions
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
def function default parameter expressions
#1
when there is an assignment of a default to an expression in a function def prototype at function definition time, can the expression use previously defined defparameters?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
I have no Python 3.8 installed now, maybe walrus operator (:=) allows this?! Not sure. However, you can build your own solution, using inspect module e.g. something like this:

import inspect
from functools import wraps

class Placeholder:
    __slots__ = ('expr', )
    def __init__(self, expr):
        self.expr=expr
        
def use_var_expr(expr):
    return Placeholder(expr)

def allow_inline_assignment(f):
    @wraps(f)
    def wrapper(*args, **kwargs):
        env = {k: v.default for k, v in inspect.signature(f).parameters.items() if not isinstance(v.default, Placeholder)}
        new_kwargs = dict()
        for k, v in inspect.signature(f).parameters.items():
            if v.default is not inspect._empty:
                if isinstance(v.default, Placeholder):
                    exec('result = ' + v.default.expr, env)
                    new_kwargs.update({k: env.get('result')})
                else:
                    new_kwargs.update({k: v.default})
        return f(*args, **new_kwargs)
    return wrapper

@allow_inline_assignment
def a(z, x=4, p=use_var_expr('x * x**2')):
    print("Positional arg: ", z)
    print("First keyword value: ", x)
    print("Second keyword value: ", p)
a(3)
Output:
Positional arg: 3 First keyword value: 4 Second keyword value: 64
Note: this solution is just for fun; it uses exec and, therefore, maybe unsafe.
Reply
#3
the whole point was to make simpler code and not have to duplicate prototype expression on the def statement. all that code isn't achieving that.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  determine parameter type in definition function akbarza 1 622 Aug-24-2023, 01:46 PM
Last Post: deanhystad
  Function parameter not writing to variable Karp 5 1,017 Aug-07-2023, 05:58 PM
Last Post: Karp
  [ERROR] ParamValidationError: Parameter validation failed: Invalid type for parameter gdbengo 3 11,287 Dec-26-2022, 08:48 AM
Last Post: ibreeden
  function with 'self' input parameter errors out with and without 'self' called dford 12 3,234 Jan-15-2022, 06:07 PM
Last Post: deanhystad
  use NULL as function parameter which is a pointer function? oyster 0 2,527 Jul-11-2020, 09:02 AM
Last Post: oyster
  When Defining a Function with an Equation as a Default Argument, which Value Is Used? OJGeorge4 4 2,732 Apr-09-2020, 08:48 AM
Last Post: DeaD_EyE
  Function with many arguments, with some default values medatib531 3 2,636 Mar-14-2020, 02:39 AM
Last Post: medatib531
  what would you call the input for the parameter(s) of a function you have defined? rix 3 2,486 Dec-16-2019, 12:04 AM
Last Post: rix
  Recursion, with "some_dict={}" function parameter. MvGulik 3 2,486 Aug-02-2019, 01:34 PM
Last Post: MvGulik
  Input as not default method parameter dan789 4 2,992 Mar-08-2019, 09:04 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020