1

CODE:

def somef(*iterable_objs, positions):
    try:
        # some statement
    except TypeError:
        print('Argument \'positions\' is missing')
        return False
>>>somef([1,2,3], 'abc', (4,5,6))

TypeError is raised because argument positions is missing.
I would like to know if there is a method how to handle this exception

4
  • What were you actually trying to do there? How did you want Python to interpret this code? It's hard to help you accomplish your goal if you only show us a failed attempt to achieve it, and you don't tell us what you were actually trying to achieve. Commented Apr 18, 2017 at 20:06
  • Did you want iterable_objs to capture all but the last positional argument and positions to capture the last? Did you want positions to be keyword-only, but have the try-except handle what happens when positions isn't provided? Were you trying to do something else? Commented Apr 18, 2017 at 20:09
  • Yes i wanted to set all given arguments into one list ( iterable_objs ) and last one is positions ONLY if key word is included Commented Apr 18, 2017 at 20:12
  • It sounds like your function should just let the TypeError propagate if positions isn't provided. Catching exceptions is a bad thing if there's nothing you can reasonably do about them, as seems to be the case here. Commented Apr 18, 2017 at 20:17

3 Answers 3

7

You can't use *args with other positional arguments following; positions is regarded as a keyword-only argument instead. Because it has no default value, it is still required.

Either give positions a default value to remove the requirement that it is always specified when calling:

def somef(*iterable_objs, positions=None):
    if positions is None:
        # calculate a default instead
        positions = range(len(iterable_objs))

or pass in a positions keyword argument explicitly:

somef([1,2,3], 'abc', positions=(4,5,6))

Side-note: never use print() to signal an error condition. The end user doesn't want to know that the programmer made a mistake, the program needs to hear about the error. Use raise SomeException() instead.

Sign up to request clarification or add additional context in comments.

4 Comments

Of course, if you're just going to raise a TypeError when positions isn't provided, you might as well just not provide a default value and let Python raise that error for you. It's not clear what the questioner intended to do when positions isn't present, beyond perhaps a general sense that they wanted to "get rid of the errors".
@user2357112: yup, which is why I left it in; using print() to flag that an argument wasn't provided is definitely not the way to go either.
@user2357112: changed heart and replaced it with a sample default that is based on the other arguments.
Well, i want to iterate through iterable_objs and align items based on positions etc.. Whole code works as i want, but i want to handle all 'possible' situations that might happen when. Thanks for advice about that Error raising!
1

As guys already mentioned if you use positions argument after *iterable_objs it is treated as keyword-only argument but if you change the order of arguments everything will work fine.

def somef(positions, *iterable_objs):

2 Comments

In this case i would follow @MartijnPieters answer and pass positions argument with explicit keyword
Why does positions have to be last? It's your code, you can put arguments in any order for functions you create.
0

If you won't always require a value to be passed for it, just supply a default:

def somef(*iterable_objs, positions=None):

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.