Python Forum
Input as not default method parameter - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Input as not default method parameter (/thread-16648.html)



Input as not default method parameter - dan789 - Mar-08-2019

Hi all.

I have a project, containing class and this method:
...
    def start(self, input="", number_occur, max_value):
        self.mem = Editor(number_occur, max_value)
...
Our testing script has pre-defined commands for our program, what means, I cannot change anything in this method structure. But this structure raises an error: non default argument follows default argument. What means that it has a problem with parameter input="" before number_occur. What to do? Do I have to change something in my python settings or?


RE: Input as not default method parameter - ichabod801 - Mar-08-2019

There is no way to fix this without either removing the default for input, or adding defaults for the other two parameters. If you can't do that because of your test script, you need to talk to your test people.


RE: Input as not default method parameter - dan789 - Mar-08-2019

Thanks, I will try.

Trying to do this whit setting parameters as default, I stucked at the point where is input="". When we have a function like this:

def function(param1, input=""):
    ...
What´s the variable of that input? How can I use it´s value inside that function?


RE: Input as not default method parameter - ichabod801 - Mar-08-2019

Within the function you reference param1 as param1, and input as input. That's what the def line does, it defines the names for the parameters given, and any defaults.

Note that input is a bad parameter name. There is a built-in function named input. If you name a parameter input, then you can't access the built-in input from within the function. That may not be a problem here, but it's a good thing to avoid in general.


RE: Input as not default method parameter - snippsat - Mar-08-2019

As mention input name is used,if not sure of build in names test it.
# Also not okay name is used 
>>> input
<built-in function input>
# foo can be use get a NameError
>>> foo
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'foo' is not defined

name 'foo' is not defined
(Mar-08-2019, 02:39 PM)dan789 Wrote: What´s the variable of that input? How can I use it´s value inside that function?
It can be None or any that user give that's the point of default arguments.
def function(param1, my_input=""):
    if my_input == '':
        return f'No argument given so i just return <{param1}> and <{repr(my_input)}>'
    return f"Ohh user want something else than '' so i return <{param1}> and <{my_input}"
Use:
# Will not get a error if give one argument
>>> function('hello')
"No argument given so i just return <hello> and <''>"

>>> function('hello', 999)
"Ohh user want something else than '' so i return <hello> and <999>"
>>> function('hello', 'world')
"Ohh user want something else than '' so i return <hello> and <world>"