Python Forum
Input as not default method parameter
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Input as not default method parameter
#1
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?
Reply
#2
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
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?
Reply
#4
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#5
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>"
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [ERROR] ParamValidationError: Parameter validation failed: Invalid type for parameter gdbengo 3 10,947 Dec-26-2022, 08:48 AM
Last Post: ibreeden
  Zfill Method Parameter Confusion new_coder_231013 3 1,045 Dec-05-2022, 05:52 PM
Last Post: new_coder_231013
  function with 'self' input parameter errors out with and without 'self' called dford 12 3,076 Jan-15-2022, 06:07 PM
Last Post: deanhystad
  [SOLVED] Input parameter: Single file or glob? Winfried 0 1,579 Sep-10-2021, 11:54 AM
Last Post: Winfried
  what would you call the input for the parameter(s) of a function you have defined? rix 3 2,441 Dec-16-2019, 12:04 AM
Last Post: rix
  def function default parameter expressions Skaperen 2 2,533 Aug-22-2019, 10:58 PM
Last Post: Skaperen
  Default setting for custom name input characteristics? beardelune 2 1,958 Aug-09-2019, 06:24 PM
Last Post: beardelune
  sqlite3 question - execute method with :parameter richalt2 2 7,462 May-20-2019, 05:35 PM
Last Post: woooee
  How to use a string method on user input Exsul 2 2,618 Mar-17-2019, 08:12 PM
Last Post: Exsul
  Input as function parameter dan789 1 2,064 Mar-08-2019, 05:19 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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