2

I have to make a function with more than 2 arguments in python and in the end I have to print the first and the last argument of the function (in a list).

I have tried like this, but it doesn't work. What am i doing wrong?

import inspect

def func(a, b, c):
    frame = inspect.currentframe()
    args, _, _, values = inspect.getargvalues(frame)
    for i in args:
        return [(i, values[i]) for i=0 and i=n]

2 Answers 2

5

There's also a way to get a variable number of function arguments in python (it's called var-positional). They then end into a list:

def func(*args): # The trick here is the use of the star
    if len(args) < 3: # In case needed, also protects against IndexError
        raise TypeError("func takes at least 3 arguments") 
    return [args[0], args[-1]]
Sign up to request clarification or add additional context in comments.

Comments

3

You are overthinking this. You already have references to the first and last arguments:

def func(a, b, c):
    print [a, c]

1 Comment

I thought that this is too simple. Thank you!

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.