Python Forum
arg and keyword arg conflict: what to raise
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
arg and keyword arg conflict: what to raise
#1
i have a function which has keyword arguments for everything. two of them also could be given as regular positional arguments. it tests for something non-None given both ways for each. the intent is to raise an exception when the call is done wrong like this. which exception should i raise for this?
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
Can you give an example of such code?
Reply
#3
Why would you do that instead of either using one or the other?
Reply
#4
(Jun-20-2020, 08:45 AM)Gribouillis Wrote: Can you give an example of such code?
def foobar(*args,**kwargs):
    if len(args)>1 and 'foo' in kwargs:
        raise Foo('"foo" is given in both positional arguments (arg 2) and keyword arguments (foo=)')
    ...

(Jun-20-2020, 11:01 AM)ndc85430 Wrote: Why would you do that instead of either using one or the other?

sometimes i want to use it one way or sometimes i want to use it the other way. i plan to post (a URL to) this code when it is done and initially tested.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#5
I would raise TypeError in such a case. For example
>>> def f():
...     pass
... 
>>> f(3)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: f() takes 0 positional arguments but 1 was given
Reply
#6
so, basically, the function being called the wrong way, is treating it as a type other than what it really is. i found a more like example random.choices() when used with an argument provided twice, raised TypeError, too. that's what i'll use.
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
  shouldn't this raise an exception? Skaperen 3 2,807 Dec-11-2021, 06:31 PM
Last Post: Skaperen
  annoying conflict: stat Skaperen 2 2,194 Dec-26-2019, 06:04 AM
Last Post: Skaperen
  venv conflict at shell prompt Skaperen 3 2,828 Feb-21-2019, 02:18 AM
Last Post: Larz60+
  import time conflict Skaperen 2 2,830 Oct-15-2017, 07:44 AM
Last Post: wavic

Forum Jump:

User Panel Messages

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