Python Forum
how to make a keyword arg w/o a default value?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
how to make a keyword arg w/o a default value?
#1
how can i define a function to have a keyword argument without a default value? think of the open() function with name= for the file name.
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
Wouldn't a keyword argument without a default value be a positional argument?
Reply
#3
If you don't do anything special, arguments will not have a default value and are able to be passed in by name. Perhaps instead of 'no default value' you're interested in it being an optional argument?
Reply
#4
If I understand correctly - you want keyword only argument, but without default value.
the closest what you can do in my opinion is using default value of None
def foo(*, name=None):
    if name is None:
        raise ValueError('Missing required keyword argument name')
    print(f'Hello {name}')

foo(name='Skaperen')
foo()
Output:
Hello Skaperen Traceback (most recent call last): File "***", line 7, in <module> foo() File "***", line 3, in foo raise ValueError('Missing required keyword argument name') ValueError: Missing required keyword argument name
if you try
foo('Skaperen')
Error:
Traceback (most recent call last): File "***", line 7, in <module> foo('Skaperen') TypeError: foo() takes 0 positional arguments but 1 was given
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
A simple way that works for most cases is to just set the default value to None. You can compare against None to see if a caller has overridden it. You couldn't tell if a caller didn't supply it or if they supplied None, but it's rare that such a case matters.

def func1(required="foo", opt1=None):
    if opt1 == None:
        print(f"opt1 was either passed in as None, or wasn't passed in at all")
    else:
        print(f"opt1 was set to {opt1}")

func1()
func1(opt1=None)
func1(opt1="text")
Output:
opt1 was either passed in as None, or wasn't passed in at all opt1 was either passed in as None, or wasn't passed in at all opt1 was set to text
If that really matters to you, or if there are a lot of optional arguments, you can just read them in as **kwargs to capture all the additional key/value pairs.

def func2(required="foo", **kwargs):
    if "opt1" not in kwargs:
        print(f"opt1 wasn't passed in at all.")
    else:
        print(f"opt1 was passed in.  It was set to {kwargs['opt1']}")

func2()
func2(opt1=None)
func2(opt1="text")
Output:
opt1 wasn't passed in at all. opt1 was passed in. It was set to None opt1 was passed in. It was set to text
Reply
#6
(Jun-29-2020, 06:34 PM)Yoriz Wrote: Wouldn't a keyword argument without a default value be a positional argument?

i'm trying to make it be a required argument that can be given either positional or keyword and be required one way or the other. but maybe i want it to be keyword only, and required. i guess Python just doesn't have that.

i guess i have to add the check to my code and can't express it as a sample call in documentation.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#7
(Jun-29-2020, 07:40 PM)Skaperen Wrote: i'm trying to make it be a required argument that can be given either positional or keyword and be required one way or the other.
well that sounds exactly like positional argument - it's required and can be supplied either as positional or keyword.
the only drawback is - if you supply it as keyword all arguments after that one must be supplied as keyword as well
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#8
(Jun-29-2020, 07:40 PM)Skaperen Wrote: i'm trying to make it be a required argument that can be given either positional or keyword and be required one way or the other. but maybe i want it to be keyword only, and required. i guess Python just doesn't have that.

I don't know that I understand what you're asking. The first part says positional or keyword, and the second part says keyword only. Both are possible, just not both at the same time.
Reply
#9
so i can do open(file='foo'), just not open(file='foo','r'), though i can do open(file='foo',mode='r')?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#10
You can't do the second one because any positional arguments must precede named arguments in the call. So the bare 'r' can't come after a named argument.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Find a specific keyword after another keyword and change the output sgtmcc 5 838 Oct-05-2023, 07:41 PM
Last Post: deanhystad
  Trying to search and make new column from the keyword. dgarg 1 1,491 Dec-20-2021, 08:41 PM
Last Post: deanhystad
  How to make the python default language be 3.6 instead of 2.7 sylas 4 6,835 Jul-06-2018, 06:11 AM
Last Post: sylas

Forum Jump:

User Panel Messages

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