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


Groups > comp.lang.python > #14938

Creating very similar functions with different parameters

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder2.enfer-du-nord.net!feeder.news-service.com!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <bahamutzero8825@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.001
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'cpython': 0.05; 'function,': 0.07; 'wrapper': 0.07; 'decorator': 0.09; 'giant': 0.09; 'subject:parameters': 0.09; 'wrappers': 0.09; 'def': 0.15; '**kwargs)': 0.16; '**kwargs):': 0.16; '*args,': 0.16; 'chunks': 0.16; 'parameters,': 0.16; 'subject:similar': 0.16; 'thunderbird': 0.16; '(but': 0.21; 'versions': 0.23; 'parameters.': 0.23; 'received:209.85.213.46': 0.23; 'received :mail-yw0-f46.google.com': 0.23; 'code.': 0.26; 'windows': 0.26; "i'm": 0.27; 'function': 0.27; 'separate': 0.28; 'print': 0.29; 'version': 0.32; 'to:addr:python-list': 0.33; 'copying': 0.33; 'difference': 0.34; 'header:User-Agent:1': 0.34; 'message- id:@gmail.com': 0.34; 'function.': 0.34; 'rather': 0.35; 'similar': 0.35; 'but': 0.37; 'two': 0.37; 'received:google.com': 0.38; 'received:209.85': 0.38; 'should': 0.38; 'received:192': 0.39; 'subject:with': 0.39; 'to:addr:python.org': 0.39; "i'd": 0.40; 'similar)': 0.84; 'subject:different': 0.84; 'subject:very': 0.91
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=message-id:date:from:user-agent:mime-version:to:subject :x-enigmail-version:content-type:content-transfer-encoding; bh=drW3pGuffNxfigbd09gF0F4FyjJkUZFoWy8azNRAqnI=; b=ZhA55aL8732UaxBEi3V6S6vJefNgisQ3j5TiJADR8MXUgCSFHb2hspJMDkdPutzJEz bwQaTKtOMplL+U3h3qHD9hMlaozw9NNTRRvVYNUaZMJhfY8MlJXlOdikGWYi6kYo7Kwj aBs/63u2k+33IxYDX38HSLu2I08OC+Z9AAKng=
Date Mon, 24 Oct 2011 16:29:25 -0500
From Andrew Berg <bahamutzero8825@gmail.com>
User-Agent Mozilla/5.0 (Windows NT 6.1; WOW64; rv:7.0) Gecko/20110922 Thunderbird/7.0
MIME-Version 1.0
To "comp.lang.python" <python-list@python.org>
Subject Creating very similar functions with different parameters
X-Enigmail-Version 1.3.2
Content-Type text/plain; charset=UTF-8
Content-Transfer-Encoding 7bit
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.2194.1319491776.27778.python-list@python.org> (permalink)
Lines 27
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1319491776 news.xs4all.nl 6967 [2001:888:2000:d::a6]:58574
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:14938

Show key headers only | 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