Skip to content Skip to sidebar Skip to footer

Python Shortcut For Variable Default Value To Be Another Variable Value If It Is None

I have the following function declaration: def set_data(obj=None, name='delp', type=None): Is there a python shortcut, where type is equal with 'name' value(default r passed) if i

Solution 1:

The usual idiom is:

defset_data(obj=None, name='delp', type=None):
    iftypeisNone:
        type = name

(But don't actually use type as a variable name, it'll mask the built in function as described here: Is it safe to use the python word "type" in my code?)

Post a Comment for "Python Shortcut For Variable Default Value To Be Another Variable Value If It Is None"