Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #14938

Creating very similar functions with different parameters

Date 2011-10-24 16:29 -0500
From Andrew Berg <bahamutzero8825@gmail.com>
Subject Creating very similar functions with different parameters
Newsgroups comp.lang.python
Message-ID <mailman.2194.1319491776.27778.python-list@python.org> (permalink)

Show all headers | View raw


I want to create a decorator with two different (but very similar)
versions of the wrapper function, but without copying giant chunks of
similar code. The main difference is that one version takes extra
parameters.

def test_dec(func, extra=False):
	if extra:
		def wrapper(ex_param1, ex_param2, *args, **kwargs):
			print('bla bla')
			print('more bla')
			print(ex_param1)
			print(ex_param2)
			func(ex_param1, ex_param2, *args, **kwargs)
	else:
		def wrapper(*args, **kwargs):
			print('bla bla')
			print('more bla')
			func(*args, **kwargs)
	return wrapper

If the function I'm wrapping takes ex_param1 and ex_param2 as
parameters, then the decorator should print them and then execute the
function, otherwise just execute the function. I'd rather not write
separate wrappers that are mostly the same.

-- 
CPython 3.2.2 | Windows NT 6.1.7601.17640 | Thunderbird 7.0

Back to comp.lang.python | Previous | Next — Next in thread | Find similar | Unroll thread


Thread

Creating very similar functions with different parameters Andrew Berg <bahamutzero8825@gmail.com> - 2011-10-24 16:29 -0500
  Re: Creating very similar functions with different parameters alex23 <wuwei23@gmail.com> - 2011-10-24 20:01 -0700
  Re: Creating very similar functions with different parameters Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-10-25 03:46 +0000

csiph-web