Communities for your favorite technologies. Explore all Collectives
Stack Overflow for Teams is now called Stack Internal. Bring the best of human thought and AI automation together at your work.
Bring the best of human thought and AI automation together at your work. Learn more
Find centralized, trusted content and collaborate around the technologies you use most.
Stack Internal
Knowledge at work
Bring the best of human thought and AI automation together at your work.
Is it possible to do something like this in Python?
def func(list, l = len(list)): print list, l
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)
list
Add a comment
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.
Required, but never shown
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.
Explore related questions
See similar questions with these tags.