Skip to content

Instantly share code, notes, and snippets.

@ChrisBeaumont
Created October 30, 2014 00:43
Show Gist options
  • Save ChrisBeaumont/320a3e2cef85641c1165 to your computer and use it in GitHub Desktop.
Save ChrisBeaumont/320a3e2cef85641c1165 to your computer and use it in GitHub Desktop.
Functions with lots of inputs in python
"""
Weird stuff happens when you define python functions with lots of inputs
"""
from subprocess import check_call
def make_file(N=500, out='tmp.py'):
args = ['a_%i' % i for i in range(N)]
with open(out, 'w') as outfile:
outfile.write('N=%i\n' % N)
outfile.write('def s(%s)' % (','.join(args)))
outfile.write(':\n')
outfile.write(' return %s' % ('+'.join(args)))
def test_py2(N):
make_file(N)
check_call(['python', '-c' 'from tmp import N, s; args = list(range(N)); s(*args)'])
def test_py3(N):
make_file(N)
check_call(['python3', '-c' 'from tmp import N, s; args = list(range(N)); s(*args)'])
for N in [100, # ok on py2/3
200, # ok on py2/3
500, # ok on py2, syntax error on py3
10000, # ok on py2, syntax error on py3
1000000, # prone to segfault on py2, syntax error on py3
]:
print "\n\n\nMaking function with %i arguments" % N
try:
test_py2(N)
print ' Python 2 OK'
except Exception as e:
print ' Python 2 Failed'
print(e)
try:
test_py3(N)
print ' Pytho 3 OK'
except Exception as e:
print ' Python 3 Failed'
print(e)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment