Why Can't Non-Default Arguments Follow Default Arguments

Why can't non-default arguments follow default arguments?

All required parameters must be placed before any default arguments. Simply because they are mandatory, whereas default arguments are not. Syntactically, it would be impossible for the interpreter to decide which values match which arguments if mixed modes were allowed. A SyntaxError is raised if the arguments are not given in the correct order:

Let us take a look at keyword arguments, using your function.

def fun1(a="who is you", b="True", x, y):
... print a,b,x,y

Suppose its allowed to declare function as above,
Then with the above declarations, we can make the following (regular) positional or keyword argument calls:

func1("ok a", "ok b", 1)  # Is 1 assigned to x or ?
func1(1) # Is 1 assigned to a or ?
func1(1, 2) # ?

How you will suggest the assignment of variables in the function call, how default arguments are going to be used along with keyword arguments.

>>> def fun1(x, y, a="who is you", b="True"):
... print a,b,x,y
...

Reference O'Reilly - Core-Python
Where as this function make use of the default arguments syntactically correct for above function calls.
Keyword arguments calling prove useful for being able to provide for out-of-order positional arguments, but, coupled with default arguments, they can also be used to "skip over" missing arguments as well.

SyntaxError: non-default argument follows default argument

Let me clarify two points here:

  • Firstly non-default argument should not follow the default argument, it means you can't define (a='b', c) in function.
    The correct order of defining parameter in function are:
    • positional parameter or non-default parameter i.e (a, b, c)
    • keyword parameter or default parameter i.e (a='b', r='j')
    • keyword-only parameter i.e (*args)
    • var-keyword parameter i.e (**kwargs)
def example(a, b, c=None, r="w", d=[], *ae,  **ab):

(a,b) are positional parameter

(c=none) is optional parameter

(r="w") is keyword parameter

(d=[]) is list parameter

(*ae) is keyword-only

(*ab) is var-keyword parameter

so first re-arrange your parameters

  • now the second thing is you have to define len1 when you are doing hgt=len1
    the len1 argument is not defined when default values are saved, Python computes and saves default values when you define the function
    len1 is not defined, does not exist when this happens (it exists only when the function is executed)

so second remove this "len1 = hgt" it's not allowed in python.

keep in mind the difference between argument and parameters.

Non-default argument follows default argument while defining a function

The error message means you can't have bins=None before non-optional arguments in the list of arguments to the function. Either make bins non-optional by removing the default =None or move it to a position after the mandatory arguments.

Meaning of SyntaxError: non-default argument follows default argument

You need to put your parameter text='' after all parameters that aren't optional (i.e., arguments that don't have default values). From the documentation:

If a parameter has a default value, all following parameters up until the “*” must also have a default value — this is a syntactic restriction that is not expressed by the grammar.

class button():
def __init__(self, color, x, y, width, height, color_font, text=''):
...

SyntaxError: non-default argument follow default argument when argument is a string

Put the filename argument before the cnvs argument, i.e.

# analysis.py

class Analysis():
def __init__ (self,cnvs):
self.cnvs = cnvs
def gene_affected_generator(self, file_name, cnvs=None)
cnvs = self.cnvs

# Some code here
# file_name is used at the end of the function
pre_names.append(file_name + '_' + Name)
return

Non-Default parameter follows default parameters

You can't have a non-positional argument (param=value) prior to positional arguments.

def func(positional1, positional2, nonpositional=10)

This is due primarily to the fact you are not required to specify the name of non-positional parameters.

func(10, 50, nonpositional=60) == func(10, 50, 60)

why can't default argument depend on non-default argument?

The C++ standard forbids it.

dcl.fct.default

9 default argument is evaluated each time the function is called with
no argument for the corresponding parameter. A parameter shall not
appear as a potentially-evaluated expression in a default argument.
Parameters of a function declared before a default argument are in
scope and can hide namespace and class member names.

[ Example:

int a;
int f(int a, int b = a); // error: parameter a
// used as default argument
typedef int I;
int g(float I, int b = I(2)); // error: parameter I found
int h(int a, int b = sizeof(a)); // OK, unevaluated operand

— end example ]

Note that default arguments are replaced at the call site if not provided

Intro.execution (emphasis mine)

11: [ Note: The evaluation of a full-expression can include the evaluation of subexpressions that are not lexically part of the
full-expression
. For example, subexpressions involved in evaluating
default arguments ([dcl.fct.default]) are considered to be created
in the expression that calls the function,
not the expression that
defines the default argument. — end note ]


You can simply overload the constructor and delegate it:

class MyClass {
explicit MyClass(unsigned int dimension)
: MyClass(dimension, unitaryVector(dimension)) //delegation
{ }
MyClass(unsigned int dimension, std::vector vector);
};

Footnote: Its a good thing to make single argument constructors explicit



Related Topics



Leave a reply



Submit