1

Is it possible to do something like this in Python?

def func(list, l = len(list)):
    print list, l

2 Answers 2

4

This is usually how it is done

def func(list, l = None):
    if l is None:
        l = len(list)
    print list, l

As a side note, avoid using built-in type/function names as your variable names(like list here)

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

Comments

1

This is as close as you can get:

def func(list, l = None):
    print list, l if l is not None else len(l)

The default parameters are evaluated when the function is first encountered. Since list is not known at that point, this is not what you want.

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.