Wandering Thoughts archives


A Python limit I never expected to run into

I've known for a while that the Python interpreter has a few little internal limits and implementation quirks. I can accept that; limits on the sizes of internal objects are not unexpected, and you have to draw the line somewhere.

But I was surprised to discover that the Python interpreter has a limit on how many arguments you can write in a function call; 255, as it happens.

(The limitation is on explicit arguments in the source code; you can call functions with many more arguments if you build a list and then use 'func(*lst)' notation.)

You might ask how anyone writes a function call with 255 arguments. In my case, one argument at a time; the function in question compiles a bunch of IP netblocks and ranges, in string form, into a set type object that other IP addresses get matched against. When I was only putting a few netblocks in the set, having them be function arguments made sense. (In fact I think that it still makes sense; I have to list them somehow, unless I exile them to a separate data file, so I might just do it directly as function arguments.)

I probably should have noticed that something was wrong earlier, but I'm bad at noticing my programs growing unless they do it in large jumps. If they just grow by a function here and some entries in a list there, without forcing me to step back for an overall view, a few hundred lines can transform into a two thousand line hulk without me really noticing.

The nitty gritty details

Because I got curious and looked it up in the CPython source: the actual limit is 255 positional arguments plus an additional 255 keyword arguments. It exists because of how the argument counts are encoded into the Python bytecode; each one is effectively restricted to a byte. Explicit func(*lst) style function invocation sidesteps this.

Some quick experimentation with compile() suggests that there is no limit on how many parameters a function can be declared with.

python/MaxFunctionArgs written at 00:31:58; Add Comment

Page tools: See As Normal.
Login: Password:
Atom Syndication: Recent Pages, Recent Comments.

This dinky wiki is brought to you by the Insane Hackers Guild, Python sub-branch.