Instead of a complicated, long expression as one statement in Python, it tends to be better to break it into several. Code is much clearer, if a few lines longer.

Keyword args in Python help a lot, but they need a mechanism to make sure caller doen't accidentally provide value for a keyword argument.

Further, hiding arguments as object state is also often a recipe for bugs, but it's also often the right way to go.

Programming is difficult.

@liw In some Python code I've seen a pattern of taking **kwargs and using kwargs.pop to extract each allowed name. If you do that, then excess positional arguments will raise TypeError instead of being quietly assigned to keyword arguments. Of course then you have to turn any KeyError raised by pop into a TypeError yourself, and often you'd want to raise a TypeError if kwargs isn't empty after you've popped all the arguments you expected. I agree, programming is hard. 😅

@jamey Yeah, I've used that. It works, but I'd prefer it if Python didn't allow positional argument values to become keyword argument values.

Jamey Sharp
Follow

@liw Heck, I'd make a lot of choices differently if I were redesigning Python today, but then probably most people who like Python would hate what I'd do to it 😉

Sign in to participate in the conversation
Toot.Cat

A Mastodon instance for cats, the people who love them, and kindness in general. We strive to be a radically inclusive safe space. By creating an account, you agree to follow our CoC below.

Instance Administration

  • Woozle: Supreme Uberwensch, general support, web hostess
  • Charlotte: tech support, apprentice in warp-drive arcana (aka Mastomaintenance)
  • ash: backend stuff, gay crimes

The Project: