Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

create a decorator for adding default keyword arguments to a function #5405

Closed
mwhansen opened this issue Feb 28, 2009 · 24 comments
Closed

create a decorator for adding default keyword arguments to a function #5405

mwhansen opened this issue Feb 28, 2009 · 24 comments

Comments

@mwhansen
Copy link
Contributor

The typical usage of this decorator would be to be applied above a
:obj:cached_method or :obj:cached_function decorator so that
the correct cached object is returned.

CC: @mwhansen @jasongrout

Component: misc

Author: Tim Dumol

Reviewer: Mike Hansen

Merged: sage-4.4.2.alpha0

Issue created by migration from https://trac.sagemath.org/ticket/5405

@nthiery
Copy link
Contributor

nthiery commented Mar 18, 2009

comment:1

Attachment: trac_5405.patch.gz

Hi Mike!

I am not so sure about the name, although I can't propose much better than
default_values, or set_default_values.

Could it be generalized to handle both positional and non positional arguments?

I'd suggest to have the doc by starting with what the thing actually does, followed by the typical usage.
Speaking of which: could you add an example of this typical usage? (it was not clear to me).

@nthiery
Copy link
Contributor

nthiery commented Apr 13, 2009

comment:3

Oops, should have set the subject to needs work. Done.

@TimDumol
Copy link
Mannequin

TimDumol mannequin commented Oct 23, 2009

comment:5

Sorry if I don't get this right, but doesn't functools.partial already fulfill this purpose?

@jasongrout
Copy link
Member

comment:6

Do you mean something like this?

from functools import partial

def partial_dec(*args, **kwds):
    def p(f):
        return partial(f,*args,**kwds)
    return p
    
@partial_dec(b=2)
def f(a,b):
    return 10*a+b

f(4)

@TimDumol
Copy link
Mannequin

TimDumol mannequin commented Oct 23, 2009

comment:7

Actually I meant something like this:

from functools import partial

@partial(partial, b=4)
def f(a,b):
    return 10*a + b

f(4)

@jasongrout
Copy link
Member

comment:8

Cute. Very nice!

@jasongrout
Copy link
Member

comment:9

So now can you use @wraps or something so that g? works correctly below?

from functools import partial, wraps

@partial(partial, b=4)
def g(a,b):
    """Docs"""
    return 10*a + b

g?

@TimDumol
Copy link
Mannequin

TimDumol mannequin commented Oct 23, 2009

comment:10

This works, but it certainly isn't obvious:


from functools import partial, wraps

@partial(lambda x: wraps(x)(partial(partial, b = 4))(x))
def g(a,b):
    """Docs"""
    return 10*a + b

print(g(5))

g?

@jasongrout
Copy link
Member

comment:11

and at that point, I'd say

@default_keywords...
def g...

is nicer. However, one might use partial in the above decorator. I think our discussion is evidence for the usefulness of the idea on this ticket.

@TimDumol
Copy link
Mannequin

TimDumol mannequin commented Oct 23, 2009

comment:12

Yep. It's certainly much clearer. Using partial should deal with the positional and non-positional arguments thing.

@jasongrout
Copy link
Member

comment:13

So we've agreed that we should create a partial decorator that allows something like:

from sage.misc.decorators (or wherever) import partial

@partial(*args, **kwds) # Same as calling partial(g, *args, **kwds) and wrapping with @wraps
def g(a,b):
   ...

just works as expected.

@TimDumol
Copy link
Mannequin

TimDumol mannequin commented Oct 23, 2009

comment:14

Perhaps a name of curry [1] would be better, since it prevents name collision with functools.partial? Then again, it supersedes functools.partial anyways.

[1] http://en.wikipedia.org/wiki/Currying

@nthiery
Copy link
Contributor

nthiery commented Oct 23, 2009

comment:15

Thanks much for pointing out functools.partial and functool.wrapper; I have several other use cases for them!

Replying to @TimDumol:

Perhaps a name of curry [1] would be better, since it prevents name collision with functools.partial? Then again, it supersedes functools.partial anyways.

[1] http://en.wikipedia.org/wiki/Currying

I prefer partial, since curry does not really encompass the specialization of named arguments.
It's really functools.partial, but made into a decorator.

@nthiery

This comment has been minimized.

@TimDumol
Copy link
Mannequin

TimDumol mannequin commented Oct 24, 2009

comment:16

I prefer partial, since curry does not really encompass the specialization of named arguments.
It's really functools.partial, but made into a decorator.

Fair enough -- but just to clarify, functools.partial is a decorator, just that it doesn't update the documentation string.

@TimDumol
Copy link
Mannequin

TimDumol mannequin commented Dec 5, 2009

Attachment: trac_5405-decorator-partial.patch.gz

Adds module sage.misc.decorators with content specialize.

@TimDumol
Copy link
Mannequin

TimDumol mannequin commented Dec 5, 2009

comment:17

Nevermind, functools.partial is not a decorator. My apologies.

This patch should do the trick. I named it specialize rather than partial, since partial conflicts with functools.partial.

@TimDumol TimDumol mannequin added s: needs review and removed s: needs work labels Dec 5, 2009
@mwhansen
Copy link
Contributor Author

mwhansen commented May 5, 2010

comment:18

Looks good to me.

@mwhansen
Copy link
Contributor Author

mwhansen commented May 5, 2010

Author: Tim Dumol

@mwhansen
Copy link
Contributor Author

mwhansen commented May 5, 2010

Reviewer: Mike Hansen

@sagetrac-mvngu
Copy link
Mannequin

sagetrac-mvngu mannequin commented May 8, 2010

Attachment: trac_5405-decorator-partial.2.patch.gz

same as previous but with username

@sagetrac-mvngu
Copy link
Mannequin

sagetrac-mvngu mannequin commented May 8, 2010

comment:19

The patch trac_5405-decorator-partial.2.patch is the same as Tim's patch, but with his username.

@sagetrac-mvngu
Copy link
Mannequin

sagetrac-mvngu mannequin commented May 8, 2010

Merged: sage-4.4.2.alpha0

@sagetrac-mvngu
Copy link
Mannequin

sagetrac-mvngu mannequin commented May 8, 2010

comment:20

Merged trac_5405-decorator-partial.2.patch.

@sagetrac-mvngu sagetrac-mvngu mannequin removed the s: positive review label May 8, 2010
@sagetrac-mvngu sagetrac-mvngu mannequin closed this as completed May 8, 2010
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants