This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: add decorator for make functions partial applicable
Type: enhancement Stage: resolved
Components: Library (Lib) Versions: Python 2.6
process
Status: closed Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: Arvin.Moezzi, r.david.murray
Priority: normal Keywords:

Created on 2012-08-16 08:34 by Arvin.Moezzi, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Messages (5)
msg168356 - (view) Author: Arvin Moezzi (Arvin.Moezzi) Date: 2012-08-16 08:34
I am not sure if this is the right way to do it but IMHO it would be great to
have a function decorator/transformer to make functions partial applicable
using functools.partial. Like


from functools import partial

class partial_applicable():
	def __call__(self, func):
		def __wrapper(*args, **kvargs):
			try:
				return func(*args, **kvargs)
			except TypeError:
				return partial(func, *args, **kvargs)

		return __wrapper

Then you could do like:

@partial_applicable()
def substract(left, right):
	return left - right

substract(10, 100) 
 => -90

rclose = substract(right = 1000)
 => rclose(10) => -990

lclose = substract(1)
 => lclose(10) => -9

What do you think?
msg168358 - (view) Author: Arvin Moezzi (Arvin.Moezzi) Date: 2012-08-16 08:44
Or maybe even

class partial_applicable():
	def __call__(self, func):
		def __wrapper(*args, **kvargs):
			try:
				return func(*args, **kvargs)
			except TypeError:
				partial_func = partial(func, *args, **kvargs)
				return partial_applicable()(partial_func)

		return __wrapper
msg168382 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2012-08-16 15:23
YAGNI, is what I think.  Or if you do need it, put it in your application.  (To tell you the truth, it just looks confusing to me...it strikes me as too magical.)

Regardless, this is more of a python-ideas kind of issue, so I suggest raising it there if you want to pursue it.
msg168385 - (view) Author: Arvin Moezzi (Arvin.Moezzi) Date: 2012-08-16 15:38
Thanks for your feedback.
msg168386 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2012-08-16 15:43
Thanks for your suggestion, even though I'm rejecting the suggestion as a bug tracker issue.  (I should have said that at the start of my answer.)
History
Date User Action Args
2022-04-11 14:57:34adminsetgithub: 59888
2012-08-16 15:43:47r.david.murraysetmessages: + msg168386
2012-08-16 15:38:57Arvin.Moezzisetmessages: + msg168385
2012-08-16 15:23:09r.david.murraysetstatus: open -> closed

nosy: + r.david.murray
messages: + msg168382

stage: resolved
2012-08-16 08:44:58Arvin.Moezzisetmessages: + msg168358
2012-08-16 08:34:39Arvin.Moezzicreate