Skip to content

RESOURCES / BLOG

How to skip an optional function argument using a ternary operator in Python?

You have a function with optional parameters and you want to pass one of them only when a condition is true. In many languages, it feels natural to try an inline conditional and move on. In Python, the details matter: passing None is not the same as skipping an argument entirely, and there is no syntax to “omit” an argument from inside a single expression.

Hi all,
I have a function with optional keyword arguments, and I only want to pass one when a condition holds. I tried using a ternary at the call site but Python still expects a value. What is the correct pattern?

Concretely: How to skip an optional function argument using a ternary operator in Python? I want something concise that reads nicely in code reviews.

Short answer: you cannot omit an argument from inside a single argument expression. Python evaluates the argument list before the function is called, so an inline expression must still produce a value. Long answer? Let’s break it down.

def f(a, b=10):
    return a + b

cond = True
result = f(5, b=20) if cond else f(5)Code language: PHP (php)

This keeps call sites explicit and works well for one or two options. It is also the most readable in many codebases.

Build a small dictionary conditionally and unpack it with **. This lets you stay on one line while truly skipping the argument.

def f(a, b=10, c=0):
    return a + b + c

cond_b = True
cond_c = False
result = f(5, **({'b': 20} if cond_b else {}), **({'c': 7} if cond_c else {}))Code language: PHP (php)
def f(a, b=10, c=0, d=None):
    ...

kwargs = {}
if use_b:
    kwargs['b'] = 20
if limit is not None:
    kwargs['c'] = limit
if label:
    kwargs['d'] = label

result = f(5, **kwargs)Code language: JavaScript (javascript)

This scales better and stays clean when several parameters are conditional.

Passing None is not the same as skipping. If you need to know whether the caller supplied a value, use a private sentinel.

SENTINEL = object()

def f(a, b=SENTINEL):
    if b is SENTINEL:
        b = compute_default()
    ...

Now callers can pass b=None as a legitimate value without being conflated with the default case.

  • Trying f(x, b=value if cond else None) will pass None, not omit the argument.
  • Using positional parameters makes it harder to skip just one in the middle. Prefer using keyword arguments instead.
  • Side effects inside inline conditionals can hurt readability. Keep it simple.
def read_image(path, color_mode="rgb", size=None, quality=None):
    # load image, maybe resize and re-encode
    ...

# 1) Two-call ternary
img = read_image("cat.jpg", size=(640, 640)) if need_resize else read_image("cat.jpg")

# 2) Inline kwargs unpack
img = read_image("cat.jpg", **({'size': (640, 640)} if need_resize else {}),
                **({'quality': 85} if compress else {}))Code language: PHP (php)

If you are uploading or transforming images in Python, you can apply the kwargs pattern with the Cloudinary Python SDK. For example, conditionally pass transformation options only when needed. This pairs nicely with workflows for saving images in Python and delivering optimized assets via an image URL.

from cloudinary.uploader import upload

to_webp = True
cap_quality = 80
folder = "demos"

opts = {"folder": folder}
opts |= {"format": "webp"} if to_webp else {}
opts |= {"quality": cap_quality} if cap_quality is not None else {}

res = upload("cat.jpg", **opts)
print(res["secure_url"])Code language: JavaScript (javascript)

Deciding whether to convert formats can also be informed by image goals.

  • You cannot omit an argument from within a single argument expression in Python.
  • Use either a ternary that chooses between two full function calls or conditionally build **kwargs.
  • For multiple options, assemble a dict and unpack it. For function internals, use a sentinel to distinguish “omitted” from None.
  • The same patterns work with SDKs like Cloudinary’s to conditionally apply transformations and formats at upload time.

Ready to streamline your media pipeline with conditional parameters and on-the-fly transformations? Create your free Cloudinary account and start building.

Start Using Cloudinary

Sign up for our free plan and start creating stunning visual experiences in minutes.

Sign Up for Free